描述

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. 数据库备份出现警告:Warning: Using a password on the command line interface can be insecure. Warning: A partial dump from a server that has GTIDs will by default include the GTIDs of all transactions, even thos

    1.先来看原备份数据库语句: mysqldump -h 127.0.0.1 -uroot -ppassword database > /usr/microStorage/dbbackup/cap ...

  2. mongodb,Mysql,redis基础教程

    数据库基础 1:mongodb基础教程 1:pymongo基础教程  2:Mysql基础教程 3:redis基础教程

  3. 1. dubbo概述

    dubbo简介: 官网:http://dubbo.io 最大程度进行解耦,降低系统耦合性,可以跨工程,跨项目; 生产者/消费者模式; jdk:1.6以上 maven:3.0以上 国际maven仓库:h ...

  4. git 修改默认编辑器

    vim,notepad(windows自带),notepad++ 当然要选notepad++ 1.首先下载notepad++ 2.将notepad++安装目录放到path中 3.git config ...

  5. Django 国际化和本地化

    所谓的国际化,是指使用不同语言的用户在访问同一个网站页面时能够看到符合其自身语言的文本页面. 国际化的基本原理是: 浏览器通过LANGUAGE_CODE在HTTP请求头中告诉网站后台服务器用户所需要的 ...

  6. C#引用出错

    今天有朋友问我为什么自己引用了配置文件,但是还不能使用配置文件呢? 之后我查看他的项目,后来发现如下问题,并且总结引用文件流程如下: 引用文件的完整程序如下: 用配置文件举例 项目中的引用右击,然后点 ...

  7. leecode第十四题(最长公共前缀)

    class Solution { public: string longestCommonPrefix(vector<string>& strs) { string res=&qu ...

  8. HYPERSPECTRAL IMAGE CLASSIFICATION USING TWOCHANNEL DEEP CONVOLUTIONAL NEURAL NETWORK阅读笔记

    HYPERSPECTRAL IMAGE CLASSIFICATION USING TWOCHANNEL  DEEP  CONVOLUTIONAL NEURAL NETWORK 论文地址:https:/ ...

  9. WebStorm Error : program path not specified

    1.出现这个错误是由于没有设置Node.js路径引起的. 2.下载安装Node.js. 3.设置对应的路径,设置后点一下Enable按钮即可. 以上,完.

  10. 如何解决failed to load the jni shared library问题

    如何解决failed to load the jni shared library问题  首先,我们来查看JDK是多少位的,在搜索框中输入cmd,然后打开命令行窗口.  在命令行中输入java -ve ...