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.

解题思路:

JAVA实现如下:

    public int hammingWeight(int n) {
int res = 0;
while(n!=0){
res += n & 1;
n >>>= 1;
}
return res;
}

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

  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 returns the number of ’1' bits it has (also know ...

  6. LeetCode 191 Number of 1 Bits

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

  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. [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. BZOJ-1433 假期的宿舍 最大流+基础建图

    网络流练习ing.. 1433: [ZJOI2009]假期的宿舍 Time Limit: 10 Sec Memory Limit: 162 MB Submit: 1748 Solved: 765 [S ...

  2. 宿主机( win 7 系统) ping 虚拟机VMware( cent os 6.6 ) 出现“请求超时”或者“无法访问目标主机”的解决方法

    首先虚拟机的网络连接设置为"Host-only": 然后在 cmd 窗口中查看 VMnet1 的 ip 地址,这里是 192.168.254.1 接下来在 Linux 中设置网卡地 ...

  3. spring获取ApplicationContext对象的方法——ApplicationContextAware

    一. 引言 工作之余,在看一下当年学的spring时,感觉我们以前都是通过get~ set~方法去取spring的Ioc取bean,今天就想能不能换种模型呢?因为我们在整合s2sh时,也许有那么一天就 ...

  4. Modular Query

    Solution F(L, R) 就是在A[L]在[L+1, R]内从左模到右. 首先应当注意到: 对$a, b > 0$ \[a \mod b \begin{cases} = a, & ...

  5. 部署搭建 Saltstack(centos6.6)

    SaltStack介绍 官网:https://docs.saltstack.com/en/latest/ 中国saltstack用户组http://www.saltstack.cn/ 下图是它的子系统 ...

  6. Laravel教程 八:queryScope 和 setAttribute

    Laravel教程 八:queryScope 和 setAttribute 此文章为原创文章,未经同意,禁止转载. Laravel Eloquent Database 直接就是按照上一节所说的那样,我 ...

  7. xcode6.3插件失效

    1.打开终端,输入以下代码:defaults read /Applications/Xcode.app/Contents/Info DVTPlugInCompatibilityUUID 2.获取到DV ...

  8. java开发微信公众平台备忘

    简单记录下前段时间开发的电子书的 公众平台的一些备忘及开发心得经验等 eclipse的一些技巧: 1.ctrl+shift+o 自动添加必要import空间及移除无用import 项目备忘+说明 1. ...

  9. WPF TabControl 模拟动画

    using System; using System.Threading; using System.Windows; using System.Windows.Controls; using Wan ...

  10. js中查找一个字符是否存在。

    <script> var a = 'd'; var re = a.indexOf('d'); ){ alert('存在'); } else { alert('不存在'); } </s ...