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. c#语言学习笔记(1)

    环境:VS Express 2013 for Desktop 也可以vs社区版,不过学习的话,Express本版做一些小的上位机工具应该是够用了 学习的网站:https://www.runoob.co ...

  2. Bootstrap内辅助类,响应式工具,组件的个人总结

    辅助类(工具类): 文本颜色: <p class="text-muted">Fusce dapibus, tellus ac cursus commodo, torto ...

  3. 如何抓取微信小程序的源码?

    一.引言: 在工作中我们会想把别人的代码直接拿过来进行参考,当然这个更多的是前端代码的进行获取. 那么微信小程序的代码怎么样获取呢?  参考 https://blog.csdn.net/qq_4113 ...

  4. XA 事务

    4.11.3 什么是XA 事务? <数据库程序员面试笔试宝典>第4章数据库基础,本章主要介绍数据库基础部分的面试题,比较适合应届毕业生,也适合由其他岗位转数据库岗位的人员.本节为大家介绍什 ...

  5. virtualenvwrapper 方便的virtualenv 包装

      virtualenvwrapper 是一个方便的virtualenv 包装我们可以用来方便的管理python 的开发环境,同时 也支持对于项目的管理 安装 pip 安装 pip install v ...

  6. THUWC2020 划船记

    PS:THUWC2020在2019年 Day 1 考场外的太懒了不写了. 三题题目大意: T1: T2: 给定一个\(n(\leq 10^5)\)个结点的有向图,每条边有个limit,表示经过这条边l ...

  7. 选择排序python实现

    选择排序(Selection sort)是一种简单直观的排序算法.它的工作原理是每一次从待排序的数据元素中选出最小(或最大)的一个元素,存放在序列的起始位置,直到全部待排序的数据元素排完.注意每次查找 ...

  8. Myschool试题

    题目: 1.查询所有学生记录,包含年级名称2.查询S1年级下的学生记录 一.com.myschool.dao 1 BaseDao package com.myschool.dao; import ja ...

  9. [总结] MSF攻击数据库服务

    0x01 攻击Mysql服务 1.1 目标探测 auxiliary/scanner/mysql/mysql_version 常用于内网中的批量mysql主机发现: 1.2 爆破登录 auxiliary ...

  10. windows环境下安装nginx

    (1)下载 官网:http://nginx.org/en/docs/windows.html 下载:http://nginx.org/en/docs/windows.html 点击:nginx/Win ...