365. Count 1 in BinaryLintCode java
Posted phdeblog
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了365. Count 1 in BinaryLintCode java相关的知识,希望对你有一定的参考价值。
Description
Count how many 1
in binary representation of a 32-bit integer.
Example
Given 32
, return 1
Given 5
, return 2
Given 1023
, return 9
Challenge
If the integer is n bits with m 1 bits. Can you do it in O(m) time?
解题:很简单,但是要考虑范围的问题。代码如下:
public class Solution {
/*
* @param num: An integer
* @return: An integer
*/
public int countOnes(int num) {
// write your code here
int count = 0;
long temp = (long)num;
if(temp < 0){
temp = (long)Math.pow(2,32) + temp;
}
while(temp != 0){
if(temp %2 == 1)
count++;
temp = temp / 2;
}
return count;
}
};
以上是关于365. Count 1 in BinaryLintCode java的主要内容,如果未能解决你的问题,请参考以下文章
Support for TLS 1.0 and 1.1 in Office 365
Get formContext inside Webresource in Dynamics 365
创建一个dynamics 365 CRM online plugin - Images in Plugin