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的个数的更多相关文章

  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 ...

  2. Leetcode#191. Number of 1 Bits(位1的个数)

    题目描述 编写一个函数,输入是一个无符号整数,返回其二进制表达式中数字位数为 '1' 的个数(也被称为汉明重量). 示例 : 输入: 11 输出: 3 解释: 整数 11 的二进制表示为 000000 ...

  3. 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 ...

  4. 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 ...

  5. LeetCode 191 Number of 1 Bits

    Problem: Write a function that takes an unsigned integer and returns the number of '1' bits it has ( ...

  6. 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 ...

  7. (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 ...

  8. 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 ...

  9. 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 ...

随机推荐

  1. 案例实战之如何写一个webpack plugin

    案例实战之如何写一个webpack plugin 1.写一个生成打包文件目录的file.md文件 // 生成一个目录项目目录的文件夹 class FileListPlugin { constructo ...

  2. hak的使用

    autohotkey简称ahk 它是一款轻量级的脚本语言文件,它可以干任何事情,如做dnf的连发脚本,类似按键精灵的自动化点击,按键自动打开文件一系列事情,文件需要按照ahk自己的语言,实现自定义的脚 ...

  3. WIFI信道频率对应

    802.11b/g还是802.11b/g/n 一般都支持13个信道 信道也称作通道(Channel).频段,是以无线信号(电磁波)作为传输载体的数据信号传送通道.无线网络(路由器.AP热点.电脑无线网 ...

  4. centos7部署postgresql集群高可用 patroni + etcd 之patroni篇

    实验环境:centos7.4纯净版 postgres版本: 9.6.15 etcd版本:3.3.11 patroni版本:1.6.0 patroni介绍可参考:https://github.com/z ...

  5. python遇到动态函数---TypeError: unbound method a() must be called with A instance as first argument (got nothing instead)

    TypeError: unbound method a() must be called with A instance as first argument (got nothing instead) ...

  6. LeetCode 732. My Calendar III

    原题链接在这里:https://leetcode.com/problems/my-calendar-iii/ 题目: Implement a MyCalendarThree class to stor ...

  7. MySQL 开启远程链接(localhost 以外的主机)

    1.在连接服务器后,操作mysql系统数据库 命令为:  mysql -u root -p use mysql: 查询用户表命令:select  User,authentication_string, ...

  8. vue基本使用及脚手架使用

    一.基本使用: <!DOCTYPE html> <html lang="en"> <head> <meta charset="U ...

  9. 【洛谷P4931】 情侣?给我烧了!(加强版)组合计数

    挺有意思的一道题... code: #include <bits/stdc++.h> using namespace std; #define N 5000006 #define mod ...

  10. learning java NIO 之 RandomFileChannel

    import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; import java.i ...