题目:

二进制中有多少个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. CSS简写及如何优化技巧

    CSS简写就是指将多行的CSS属性简写成一行,又称为CSS代码优化或CSS缩写.CSS简写的最大好处就是能够显著减少CSS文件的大小,优化网站整体性能,更加容易阅读. 下面介绍常见的CSS简写规则: ...

  2. Android开发之计算器(一)界面设计

    计算器开发主要涉及到LinearLayout布局.EditText.Button的使用.为android入门基础内容. 打开android studio选择创建一个新的工程,应用程序的名称为Calcu ...

  3. Powerdesigner15 创建数据库生成脚本

    Ctrl + Shift + X,打开脚本编辑界面,使用VBScript编写脚本 点击帮助按钮,可以打开OLE Help,“Libraries >> PdPDM”中可以查看内置的类库.“A ...

  4. Js操作Select大全(取值、设置选中)

    Js操作Select是很常见的,也是比较实用的. jquery操作select(取值,设置选中) 每一次操作select的时候,总是要出来翻一下资料,不如自己总结一下,以后就翻这里了. 比如<s ...

  5. Eclipse 代码提示功能设置。

    1.        解决实例化时自动补全不必要的单词问题 2.        以MyEclipse 6.5重新配图 鉴 于网上的批评之声甚大,我只想说明我的想法:这样的增强代码提示,最终是用来辅助我们 ...

  6. 再也不要说,jquery动画呆板了

    1 show()方法和hide()方法 $("selector").show()  从display:none还原元素默认或已设置的display属性$("selecto ...

  7. Go在linux下的安装

    在Ubuntu.Debian 或者 Linux Mint上安装Go语言 下面是在基于Debian的发行版上使用apt-get来安装Go语言和它的开发工具. $ sudo apt-get install ...

  8. 【ajax跨域】原因原理解决

    1.安全,跨域cookie iframe 2.很简单,就是利用<script>标签没有跨域限制的“漏洞”(历史遗迹啊)来达到与第三方通讯的目的.当需要通讯时,本站脚本创建一个<scr ...

  9. SQL对like 操作中的特殊字符处理方法

    SQL对like 操作中的特殊字符处理方法:    SQL Server查询过程中,单引号 ' 是特殊字符,所以在查询的时候要转换成双单引号 '' .    在like操作还有以下特殊字符:下划线_, ...

  10. SQL SERVER 強制指定使用索引 -转载 只为学习

    今天很高兴 ,有学会了一种数据库优化的方式,哈哈 今天遇到一個查詢逾時的問題:兩段SQL,只差在WHERE,一個是WHERE COLUMN1='AAA',一個是WHERE COLUMN1='BBB', ...