描述

Write a function that takes an unsigned integer and return the number of '1' bits it has (also known as the Hamming weight).

输入是一个无符号整数,返回其二进制表达式中数字位数为 ‘1’ 的个数。

解析

消除最后的1

观察一下 n 与 n-1 这两个数的二进制表示:对于 n-1 这个数的二进制来说,相对于 n 的二进制,它的最末位的一个 1 会变成 0,最末位一个 1 之后的 0 会全部变成 1,其它位相同不变。

比如 n = 8888,其二进制为 10001010111000

则 n - 1 = 8887 ,其二进制为 10001010110111

通过按位与操作后:n & (n-1) = 10001010110000

也就是说:通过 n&(n-1)这个操作,可以起到消除最后一个1的作用。

所以可以通过执行 n&(n-1) 操作来消除 n 末尾的 1 ,消除了多少次,就说明有多少个 1 。

简单遍历

每次与&1,如果为1,说明最后一位为1,再右移一位。

代码

public class Solution {
// you need to treat n as an unsigned value
public int hammingWeight(int n) {
int cnt = 0;
while(n != 0){
cnt++;
n = n & (n - 1);
}
return cnt;
}
}
public class Solution {
// you need to treat n as an unsigned value
public int hammingWeight(int n) {
int count = 0;
while (n != 0) {
count += (n & 1);
n = n >>> 1;
}
return count;
}
}

[LeetCode] 191. Number of 1 Bits ☆(位 1 的个数)的更多相关文章

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

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

  3. 191 Number of 1 Bits 位1的个数

    编写一个函数,输入是一个无符号整数,返回的是它所有 位1 的个数(也被称为汉明重量).例如,32位整数 '11' 的二进制表示为 00000000000000000000000000001011,所以 ...

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

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

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

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

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

  8. LeetCode 191 Number of 1 Bits

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

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

随机推荐

  1. urllib模块中的方法

    urllib模块中的方法 1.urllib.urlopen(url[,data[,proxies]]) 打开一个url的方法,返回一个文件对象,然后可以进行类似文件对象的操作.本例试着打开google ...

  2. 1:Javascript的数据类型和相互转换

    第一节:JavaScript的数据类型 他是弱类型 var 但是正是由于其实弱类 所以其后台的数据类型转换也是我们值得思考的 JavaScript的数据类型有两种 一种是原始类型  另外一种是对象类型 ...

  3. ERR! registry error parsing json

    报错日志: ERR! registry error parsing json ERR! registry error parsing json 解决过程: 从github上克隆一个项目,在npm i的 ...

  4. python 部分函数

    abs(number) ,返回数字的绝对值cmath.sqrt(number) ,返回平方根,也可以应用于负数float(object) ,把字符串和数字转换为浮点数help() ,提供交互式帮助in ...

  5. 7 个最佳的 Java 框架

    毫无疑问,Java是目前最需要的编程语言之一.在这里,我们已经挖掘了一些关于框架趋势的有用信息,以减轻全球软件开发人员的日常工作. 根据RebelLabs,也是在线Java用户组(虚拟JUG)的媒体盟 ...

  6. 基于iOS用CoreImage实现人脸识别

    2018-09-04更新: 很久没有更新文章了,工作之余花时间看了之前写的这篇文章并运行了之前写的配套Demo,通过打印人脸特征CIFaceFeature的属性,发现识别的效果并不是很好,具体说明见文 ...

  7. Windows 多用户接续设置

    第1步:如果想真正实现多用户接续,请安装角色:remote desktop service.第2步: gpedit.msc,找到“计算机配置”-“管理模板”-“Windows 组件”-“远程桌面服务” ...

  8. VNPY思维导图架构

    VNPY是使用人数世界第三,国内第一的量化交易框架,封装的接口主要有ctp(期货),wind,xtp(股票)等.内部包含回测.实盘.模拟盘等模块.数据库默认为MongoDB的no-sql数据库,基于p ...

  9. Ruby 基础教程 第二部分 Ruby 的基础 第4章

    第二部分 Ruby 的基础 第4章~第6章 这一部分是 Ruby 编程需要遵守的规则. 第四章 对象,变量与常量 对象 & 类 对象的常见种类: 数值对象 字符串对象 数组.散列对象 正则表达 ...

  10. 关于TeeChart使用我会持续更新

    关于TeeChart使用我会持续更新 这篇文章中我是在Winform窗体使用TeeChart2011控件,通过定时器实现了实时绘制曲线图(三个序列). 先上一下效果图: 1.TeeChart2011. ...