[LeetCode] 191. Number of 1 Bits 二进制数1的个数
Write a function that takes an unsigned integer and returns the number of ’1' bits it has (also known as the Hamming weight).
For example, the 32-bit integer ’11' has binary representation 00000000000000000000000000001011, so the function should return 3.
写一个函数操作无符号整数,返回整数的二进制数1的个数。
解法:位操作Bit Manipulation
使用n&(n-1)的方法
假使 n =0x110101
n n-1 n&(n-1)
step1: 110101 110100 110100
step2: 110100 110011 110000
step3: 110000 101111 100000
step4: 100000 011111 000000
发现有几个1,就循环几次n&(n-1)得到0。
时间复杂度:O(M),M是n中1的个数
Java:
public class Solution {
    public int NumberOf1(int n) {
        int count = 0;
        while (n != 0) {
            n &= (n - 1);
            count ++;
        }
        return count;
    }
}  
Python:
class Solution:
# @param n, an integer
# @return an integer
def hammingWeight(self, n):
result = 0
while n:
n &= n - 1
result += 1
return result
Python:
class Solution(object):
def hammingWeight(self, n):
"""
:type n: int
:rtype: int
"""
res=0
while n:
res += (n&1)
n >>= 1
return res
Python:
class Solution(object):
def hammingWeight(self, n):
"""
:type n: int
:rtype: int
"""
b = bin(n)
return b.count('1')
C++:
class Solution {
public:
    int hammingWeight(uint32_t n) {
        int res = 0;
        for (int i = 0; i < 32; ++i) {
            res += (n & 1);
            n = n >> 1;
        }
        return res;
    }
}; 
C++:
class Solution {
public:
    int hammingWeight(uint32_t n) {
        int count = 0;
        for (; n; n &= n - 1) {
            ++count;
        }
        return count;
    }
};
类似题目:
[LeetCode] 190. Reverse Bits 翻转二进制位
All LeetCode Questions List 题目汇总
[LeetCode] 191. Number of 1 Bits 二进制数1的个数的更多相关文章
- [LeetCode] 191. Number of 1 Bits ☆(位 1 的个数)
		
描述 Write a function that takes an unsigned integer and return the number of '1' bits it has (also kn ...
 - Leetcode#191. Number of 1 Bits(位1的个数)
		
题目描述 编写一个函数,输入是一个无符号整数,返回其二进制表达式中数字位数为 '1' 的个数(也被称为汉明重量). 示例 : 输入: 11 输出: 3 解释: 整数 11 的二进制表示为 000000 ...
 - LN : leetcode 191 Number of 1 Bits
		
lc 191 Number of 1 Bits 191 Number of 1 Bits Write a function that takes an unsigned integer and ret ...
 - LeetCode 191. Number of 1 bits (位1的数量)
		
Write a function that takes an unsigned integer and returns the number of ’1' bits it has (also know ...
 - LeetCode 191 Number of 1 Bits
		
Problem: Write a function that takes an unsigned integer and returns the number of '1' bits it has ( ...
 - Java for LeetCode 191 Number of 1 Bits
		
Write a function that takes an unsigned integer and returns the number of ’1' bits it has (also know ...
 - (easy)LeetCode  191.Number of 1 Bits
		
Number of 1 Bits Write a function that takes an unsigned integer and returns the number of ’1' bits ...
 - Java [Leetcode 191]Number of 1 Bits
		
题目描述: Write a function that takes an unsigned integer and returns the number of ’1' bits it has (als ...
 - leetcode 191 Number of 1 Bits(位运算)
		
Write a function that takes an unsigned integer and returns the number of ’1' bits it has (also know ...
 
随机推荐
- git上传者姓名修改
			
只需要两个指令 git config user.name 和 git config –global user.name 在控制台中输入git config user.name获取当前的操作名称 修改名 ...
 - 08 c++中运算符重载(未完成)
			
参考:轻松搞定c++语言 定义:赋予已有运算符多重含义,实现一名多用(比较函数重载) 运算符重载的本质是函数重载 重载函数的格式: 函数类型 operator 运算符名称(形参表列) { 重载实体 ...
 - 实验八 《Coderxiaoban团队》团队作业4:基于原型的团队项目需求调研与分析
			
实验八 <Coderxiaoban团队>团队作业4:基于原型的团队项目需求调研与分析 项目 内容 这个作业属于哪个课程 任课教师博客主页链接 这个作业的要求在哪里 实验八 团队作业4:基于 ...
 - danci2
			
composite 英 ['kɒmpəzɪt] 美 [kɑm'pɑzɪt] n. 复合材料:合成物:菊科 adj. 复合的:合成的:菊科的 vt. 使合成:使混合 a composite of 网络释 ...
 - kvm创建windows2008虚拟机
			
virt-install -n win2008-fushi001 -r 16384 --vcpus=4 --os-type=windows --accelerate -c /data/kvm/imag ...
 - linux下安装Sublime Text3并将它的快捷方式放进启动器中和卸载Sublime
			
Sublime Text是一个代码编辑器,我主要是用它来编辑python.下面就来简单说明下它在linux的安装过程吧! 1.添加sublime text3的仓库 首先按下快捷键ctrl+alt+t打 ...
 - 2019-2020-1 20199302《Linux内核原理与分析》第八周作业
			
一.上课学习笔记 1.shell作用:①运行程序 ②重定向(输入/输出重定向) ③可编程(写脚本) 执行一个c程序时,如果切进另一个进程,会进入该进程而切不回原进程,所以需要为调用的进程创一个子进程. ...
 - Redis 高可用架构设计(转载)
			
转载自:https://mp.weixin.qq.com/s?__biz=MzA3NDcyMTQyNQ==&mid=2649263292&idx=1&sn=b170390684 ...
 - javascript 终极循环方法for... of ..推荐
			
js目前有很多的循环方法,如for, forEach, for .. in, for of 等等,而在ES6里面,我们又增加了一些数据结构,比如set,map,Symbol等. 那么我们该选取哪一 ...
 - 61、Spark Streaming:部署、升级和监控应用程序
			
一.部署应用程序 1.流程 1.有一个集群资源管理器,比如standalone模式下的Spark集群,Yarn模式下的Yarn集群等. 2.打包应用程序为一个jar包. 3.为executor配置充足 ...