题意:

  提供一个无符号32位整型uint32_t变量n,返回其二进制形式的1的个数。

思路:

  考察二进制的特性,设有k个1,则复杂度为O(k)。考虑将当前的数n和n-1做按位与,就会将n的最后一个1去掉,重复这样的操作就可以统计出1的个数了。(2015年春季 小米实习生的笔试题之一)

 class Solution {
public:
int hammingWeight(uint32_t n) {
int cnt=;
while(n)
{
n&=n-;
cnt++;
}
return cnt;
}
};

AC代码

python3

 class Solution(object):
def hammingWeight(self, n):
"""
:type n: int
:rtype: int
"""
cnt=0
while n:
cnt+=1
n=n&(n-1)
return cnt

AC代码

LeetCode Number of 1 Bits 计算1的个数的更多相关文章

  1. [LeetCode] Number of 1 Bits 位1的个数

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

  2. 2016.5.15——leetcode:Number of 1 Bits ,

    leetcode:Number of 1 Bits 代码均测试通过! 1.Number of 1 Bits 本题收获: 1.Hamming weight:即二进制中1的个数 2.n &= (n ...

  3. LeetCode Number of 1 Bits

    原题链接在这里:https://leetcode.com/problems/number-of-1-bits/ 题目: Write a function that takes an unsigned ...

  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 ☆(位 1 的个数)

    描述 Write a function that takes an unsigned integer and return the number of '1' bits it has (also kn ...

  6. [LeetCode] Number of 1 Bits 位操作

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

  7. [LeetCode] Number of Digit One 数字1的个数

    Given an integer n, count the total number of digit 1 appearing in all non-negative integers less th ...

  8. LeetCode——Number of 1 Bits

    //求一个整数的二进制串中1的个数 public int hammingWeight(int n) { String b_str = Integer.toBinaryString(n); int b_ ...

  9. [LeetCode] Number of Distinct Islands 不同岛屿的个数

    Given a non-empty 2D array grid of 0's and 1's, an island is a group of 1's (representing land) conn ...

随机推荐

  1. c#中Marshal.Copy()方法的使用

    c#中Marshal.Copy方法的使用 Marshal.copy()方法用来在托管对象(数组)和非托管对象(IntPtr)之间进行内容的复制 函数有很多重载,如下所示: Copy(array< ...

  2. python接口自动化(三十五)-封装与调用--流程类接口关联(详解)

    简介 流程相关的接口,主要用 session 关联,如果写成函数(如上篇),s 参数每个函数都要带,每个函数多个参数,这时候封装成类会更方便.在这里我们还是以博客园为例,带着小伙伴们实践一下. 接口封 ...

  3. rowid去重(转)

    实际应用场景:数据去重--当多条记录主键相同或者多条记录完全一致时,只需要留下一条记录 delete from bal_acctbook_info where rowid not in (select ...

  4. cf780E(dfs)

    题目链接: http://codeforces.com/problemset/problem/780/E 题意: 给出一个 n 个点 m 条边的图, 有 k 个人, 初始位置可以为任意位置, 每个人最 ...

  5. Pipenv: Python包管理神器

    Pipenv旨在将最好的包管理机制(bundler, composer, npm, cargo, yarn等等)带到Python中去. 它会为你的项目自动创建和管理virtualenv,也会在安装和移 ...

  6. 关于给gridSelect控件设置默认值

    给这个选择表格框添加默认值: <td class="form_label">                            客户名称:              ...

  7. linux日志守护进程 syslog

    几乎所有的网络设备都可以通过syslog协议,将日志信息以UDP的形式传送给远端服务器,远端接收日志服务器必须通过syslogd监听UDP端口514,并根据 syslog.conf 配置文件张的配置处 ...

  8. 关于STL库中的max min swap

    嗯...   不得不说c++中的STL库是一个神奇的东西   可以使你的代码显得更加简洁....   今天就只讲STL中的三个鬼畜:   max       min       swap   具体操作 ...

  9. Jmeter 自动化测试报告扩展(转 Todo 需要修正)

    首先了解下生成测试报告的过程,我们看到的测试报告是由.jtl格式转换为.html,html报告的样式由extras目录下xsl文件决定.优化测试报告需要分为两部分内容,首先我们要优化输出的测试内容,其 ...

  10. C# DataTable转List<T>--利用反射

    /// <summary> /// 利用反射将Datatable转换为List<T>对象 /// </summary> /// <typeparam name ...