题目:

二进制中有多少个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. 用canvas 绘制的饼状统计图、柱状统计图、折线统计图

    canvas 绘制的饼状统计图 canvas 绘制的柱状统计图 canvas 绘制的折线统计图

  2. JQuery window.opener

    $('#Save').click(function () {    var parent = $(parent.document.body);    $(parent).find('input#add ...

  3. 【Qt】QSettings介绍【转】

    简介 QSettings类提供了持久的跨平台应用程序设置. 用户通常期望应用程序记住它的设置(窗口大小.位置等)所有会话.这些信息通常存储在Windows系统注册表,OS X和iOS的属性列表文件中. ...

  4. 前端自动化构建工具——gulp

    gulp是基于流的前端自动化构建工具. 一.环境配置 gulp是基于nodejs的,所以没有 nodejs 环境的要先去安装好 然后给系统配上gulp环境 npm install -g gulp 再到 ...

  5. python之递归

    递归的定义:即对自己自身内容的引用. 有用的递归函数应包含以下几步份: 当函数直接返回值时有基本的实例(最小可能性问题): 递归实例,包括一个或者多个问题较小部分的递归调用: 递归的关键就是将问题分解 ...

  6. mariadb 10 多源复制(Multi-source replication) 业务使用场景分析,及使用方法

    mariadb 10 多源复制(Multi-source replication) 业务使用场景分析,及使用方法 官方mysql一个slave只能对应一个master,mariadb 10开始支持多源 ...

  7. libnuma.so.1()(64bit) is needed by mysql-community-server-5.7.9-1.el6.x86_64

    版本:5.7.9 新装的CentOS 6.3 安装MySQL 5.7.9 出现的问题 1.首先卸载系统自带的mysql 5.1的包    yum   -y  remove   mysql-libs-5 ...

  8. 【html5】这些新类型 能提高生产力

    <!DOCTYPE HTML> <html> <head> <meta charset="utf-8"> <title> ...

  9. (菜鸟要飞系列)五,基于Asp.Net MVC5的后台管理系统(添加数据表的分页功能)

    献上代码 ) { List<UserModel> arrayUserModel = new List<UserModel>(); string strText = Reques ...

  10. C/C++ 内联函数

    内联函数具备一般函数的性质,但是不需要调用,而是在编译阶段,会用函数体替换函数名被调用的地方.可以节省调用时间(进出栈.保存上下文). 在编译层面和宏的作用相同.内联函数的展开在编译阶段,宏展开在预处 ...