Problem:

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.

Summary:

求三十二位无符号数含1的位数。

Analysis:

1. 最简单的思路为将n移位直至n为0,每移位一次判断最低位是否为1。但若有1在最高位则需移位32次,效率太低。

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

2. 下面这种思路能够把移位的次数降低至与含1的位数相等,大大提高了效率。

举个栗子:若n = 1010010,则:

  1. n = 1010010  n - 1 = 1010001  n & (n - 1) = 1010000
  2. n = 1010000  n - 1 = 1001111  n & (n - 1) = 1000000
  3. n = 1000000  n - 1 = 0111111  n & (n - 1) = 0000000

可以看出,每进行一次n & (n - 1)操作,最低位上的1就会被消去。

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

LeetCode 191 Number of 1 Bits的更多相关文章

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

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

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

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

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

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

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

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

  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. linux下的库冲突问题

    lib1.c #include <stdio.h>int fun(){ printf("lib1\n"); return 0;} lib2.c #include < ...

  2. PHP预编译处理技术简介

    1.提高数据库的效率:减少编译次数,减少连接次数.当出现当量操作sql语句,比如大量将数据插入数据库中,原来的那种单个执行sql语句或者批量执行sql语句的做法,显然是不可行的,因为无论是单个执行还是 ...

  3. h5交互元素details标签

    details是h5新增的交互元素,details与 summary 标签配合使用可以为 details 定义标题.默认情况下,不显示 details 标记中的内容.当用户点击标题时,会显示出 det ...

  4. 【PHP面向对象(OOP)编程入门教程】11.类的继承

    继承作为面向对象的三个重要特性的一个方面,在面向对象的领域有着及其重要的作用,好像没听说哪个面向对象的语言不支持继承. 继承是PHP5面象对象程序设计的重要特性之一,它是指建立一个新的派生类,从一个或 ...

  5. 【C语言入门教程】7.5 枚举

    在实际应用中,有的变量只有几种可能取值.如人的性别只有两种可能取值,星期只有七种可能取值.在 C 语言中对这样取值比较特殊的变量可以定义为枚举类型.所谓枚举是指将变量的值一一列举出来,变量只限于列举出 ...

  6. 弹窗插件 popup.js 完美修正版

    作为信息展示弹出窗口,很有用!是一个js插件,不是jQuery插件! 地址:http://img.jb51.net/online/popup/popup.html

  7. linux下好用的软件

    搜狗输入法 http://pinyin.sogou.com/linux/ wps http://community.wps.cn/download/ 浏览器 chrome or FireFox or ...

  8. 跟着百度学PHP[4]OOP面对对象编程-13-魔术方法__set(),__get(),__isset(),__unset()

    __set() 在对象访问私有成员的时候自动被调用,达到了给你看,但是不能给你修改的效果!(在对象访问一个私有的成员的时候就会自动的调用该魔术方法) __get() 方法用于获取私有属性值.(在设置私 ...

  9. WebStorm设置左侧菜单栏背景色和样式

    WebStrom一直以来都是默认的白色主题,今天想修改了下主题皮肤,结果导致左侧项目资源栏和顶部菜单栏也变成了黑色,结果无法改变回来,网上查了各种帖子,居然也没找到解决方法,自己研究了半天,终于搞定了 ...

  10. Activity中UI框架基本概念

    Activity中UI框架基本概念 Activity 是应用程序的基本组成部分,提供了可视的界面,UI容器, 与用户进行交互: 具体Acitivity是怎么样显示这些事视图元素以及响应事件交互的. 一 ...