题目:

二进制中有多少个1

49% 通过 计算在一个 32 位的整数的二进制表式中有多少个 1.

样例

给定 32 (100000),返回 1

给定 5 (101),返回 2

给定 1023 (111111111),返回 9

解题:

Java程序:

public class Solution {
/**
* @param num: an integer
* @return: an integer, the number of ones in num
*/
public int countOnes(int num) {
// write your code here
return countOnes1(num); //总耗时: 1201 ms
// return countOnes2(num); //总耗时: 1185 ms
// return countOnes3(num); // 总耗时: 1215 ms
}
public int countOnes1(int num){
int count = 0;
while(num!=0){
if(num%2==1)
count++;
num=num/2;
}
return count;
}
public int countOnes2(int num){
int count = 0;
while(num!=0){
count +=num&0x01;
num = num>>1;
}
return count;
}
public int countOnes3(int num){
int count = 0;
while(num!=0){
num = num & (num-1);
count++;
}
return count;
}
};

上面程序中有三种方法,都来自编程之美
第一种:

原数除以2后,数字将减少一个0

若余数是1则,减少一个1,记录1的个数

若余数是0则,减少一个0,记录0的个数

第二种:

利用位运算

0&1 =0

1&1 =1

这个32位数与0000 0001 与运算的值是1,记录1的个数

即:count += num & 0x01

num右移一位

num=num>>1

第三种:

  int count = 0;
while(num!=0){
num = num & (num-1);
count++;
}
return count;

好机智的方法,时间复杂度是O(M),M是num中1的个数

Python程序:

class Solution:
# @param num: an integer
# @return: an integer, the number of ones in num
def countOnes(self, num):
# write your code here
# return self.countOnes1(num) # 387 ms
# return self.countOnes2(num) # 418 ms
return self.countOnes3(num) # 398 ms def countOnes1(self,num):
count = 0
while num!=0:
if num%2==1:
count+=1
num/=2
return count def countOnes2(self,num):
count = 0
while num!=0:
count += num&0x01
num = num>>1
return count def countOnes3(self,num):
count = 0
while num!=0:
num = num & (num-1)
count+=1
return count

lintcode :Count 1 in Binary 二进制中有多少个1的更多相关文章

  1. LintCode Count 1 in Binary

    知识点 1. 整数的二进制表示法 2. 十进制和二进制的转换 http://baike.baidu.com/view/1426817.htm 3. 负整数的表示(原码,补码,反码) http://ww ...

  2. n的二进制中有几个1

    实例十七:n的二进制中有几个1 方法:result=n & (n-1)   n&(n-1)的目的使最低位的1不断翻转. 比如:n=108,其二进制表示为0110 1100,则n& ...

  3. LintCode-365.二进制中有多少个1

    二进制中有多少个1 计算在一个 32 位的整数的二进制表式中有多少个 1. 样例 给定 32 (100000),返回 1 给定 5 (101),返回 2 给定 1023 (111111111),返回 ...

  4. # Leetcode 67:Add Binary(二进制求和)

    Leetcode 67:Add Binary(二进制求和) (python.java) Given two binary strings, return their sum (also a binar ...

  5. 762. Prime Number of Set Bits in Binary Representation二进制中有质数个1的数量

    [抄题]: Given two integers L and R, find the count of numbers in the range [L, R] (inclusive) having a ...

  6. 365. Count 1 in Binary【LintCode java】

    Description Count how many 1 in binary representation of a 32-bit integer. Example Given 32, return  ...

  7. lintcode:Add Binary 二进制求和

    题目: 二进制求和 给定两个二进制字符串,返回他们的和(用二进制表示). 样例 a = 11 b = 1 返回 100 解题: 和求两个链表的和很类似 考虑进位,考虑最后一项的进位 0+0 = 0 不 ...

  8. [LintCode] Serialize and Deserialize Binary Tree(二叉树的序列化和反序列化)

    描述 设计一个算法,并编写代码来序列化和反序列化二叉树.将树写入一个文件被称为“序列化”,读取文件后重建同样的二叉树被称为“反序列化”. 如何反序列化或序列化二叉树是没有限制的,你只需要确保可以将二叉 ...

  9. Lintcode: Count of Smaller Number

    Give you an integer array (index from 0 to n-1, where n is the size of this array, value from 0 to 1 ...

随机推荐

  1. $.each遍历json对象

    查看一个简单的jQuery的例子来遍历一个JavaScript数组对象. var json = [ {"id":"1","tagName": ...

  2. Redis源码研究--启动过程

    ---------------------6月23日--------------------------- Redis启动入口即main函数在redis.c文件,伪代码如下: int main(int ...

  3. Eclipse导入android包错误

    错误提示:Invalid project description… 解决方案:假设你的工作空间是workshop,那么你可以在你的workshop下新建一个文件夹,然后放入你的包,再在Eclipse中 ...

  4. [转]PLS-S-00201, identifier 'CALLDEMO.GET_EMPLOYEES' must be declared 预编译错误原因及解决办法

    $ proc sample9.pc SQLCHECK=SEMANTICS Pro*C/C++: Release 11.2.0.1.0 - Production on Tue Jan 8 15:18:4 ...

  5. atomic_read

    static inline int atomic_read(const atomic_t *v) { return (*(volatile int *)&(v)->counter); } ...

  6. topcoder 673

    DiV1 300:给一组士兵再给一组战马都有权值. 安排战马的顺序的方案数,是第一个士兵和其战马的权值乘积最大. 做法:随便暴力就好. 枚举战马和第一个士兵匹配.其他士兵按权值从大到小排序,战马权值按 ...

  7. Daily Scrum 11.8

    摘要:本次meeting继续讨论程序的问题以及单元测试和集成测试.本次测试为1.00版本.本次的Task列表如下: Task列表 出席人员 Today's Task Tomorrow's Task 刘 ...

  8. OC的类方法、对象方法和函数

    OC语言中的方法和函数是有区别的:类内部叫方法,单独定义的叫函数,定义的格式也不同 类方法:+ (void) 方法名.对象方法:- (void) 方法名.函数:void 函数名(参数列表) #impo ...

  9. android 讯飞语音识别(离线)注意事项

    讯飞语音识别:使用注意事项:mainfest.xml中一定要记得权限写进去21001:我的情况是没有写SpeechApp类,并且需要在application中注册:20005:无匹配结果23300:本 ...

  10. android 设置半透明

    对于Button和ImageButton 还有一些View 设置半透明或者透明都是通过 android:background="#b0000000" 这是就是半透明 android ...