题目:

二进制中有多少个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. Linux上iptables防火墙的基本应用

    1.安装iptables防火墙 yum install iptables -y 2. 清除已有的iptables规则 iptables -F iptables -X iptables -Z 3.显示i ...

  2. Scrapy源码学习(一)

    用Scrapy已经有一段时间了,觉得该是看一下源码的时候了.最开始用的时候还是0.16的版本,现在稳定版已经到了0.18.结合使用Scrapy的过程,先从Scrapy的命令行看起. 一.准备 下载源代 ...

  3. DataSnap数据库连接池,数据集对象池的应用

    传统的应用服务器的开发往往是在ServerMethods单元中拖放一堆TDataSet, TDaTaSetProvider控件,这是一个最简单粗暴的开发方向,往往会造成服务端程序文件的臃肿.服务运行期 ...

  4. css透明(支持各浏览器)

    opacity: 0.4;filter: progid:DXImageTransform.Microsoft.Alpha(opacity=40); -ms-filter: "progid:D ...

  5. Hive内表和外表的区别

    本文以例子的形式介绍一下Hive内表和外表的区别.例子共有4个:不带分区的内表.带分区的内表.不带分区的外表.带分区的外表. 1 不带分区的内表 #创建表 create table innerTabl ...

  6. 游刃于MVC、WCF中的Autofac

    为了程序的健壮性.扩展性.可维护性,依赖抽象而不是具体实现类等等,于是我选择了Autofac依赖注入容器 就是这个工厂来降低耦合.之前买东西是自己去超市,现在呢 我需要什么东西,他们给送过来直接拿到了 ...

  7. C#的winform小合集

    C#的winform小合集 博主很懒,又想记录一下自己的所做所为,仅此而已,供自己日后所看.这个是博主自主学习C#所写的一些小程序,有好玩的,也有一些无聊闲得蛋疼所作的. 内容介绍 C#入门窗口输出h ...

  8. Daject初探之Record模型

    上一篇博文我简单介绍了Daject以及Daject的Table模型,Table模型是对一张数据表的抽象,从数据表的级别处理数据,而Record模型是对单条数据记录的抽象,从记录的级别处理数据. 这一篇 ...

  9. 【学习总结】声明变量在@interface括号中与使用@property的区别

    方式一:直接在.h文件@interface中的大括号中声明. @interface Test : NSObject { NSString *str; // 私有变量 , 其他类无法访问,只能够该类内部 ...

  10. .NET开源项目介绍及资源推荐:数据持久层

    在.NET平台下,关于数据持久层框架非常多,本文主要对如下几种做简要的介绍并推荐一些学习的资源: 1.NHibernate 2.NBear 3.Castle ActiveRecord 4.iBATIS ...