题意:给你一个整数,计算该整数的二进制形式里有多少个“1”。比如6(110),就有2个“1”。

一开始我就把数字n不断右移,然后判定最右位是否为1,是就cnt++,否则就继续右移直到n为0。

可是题目说了是无符号整数,所以给了2147483648,就WA了。

因为java里的int默认当做有符号数来操作,而2147483648超过int的最大整数,所以在int里面其实是当做-1来计算的。

那么,不能在while里面判断n是否大于0,和使用位操作符>>。应该使用位操作符>>>,这个操作符是对无符号数进行右移的。

public class Solution {
// you need to treat n as an unsigned value
public int hammingWeight(int n) {
int cnt = 0;
for(int i = 0; i < 32; i++) {
if( ((n>>>i)&1) == 1 ) cnt++;
}
return cnt;
}
}

 

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

  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. LeetCode 191 Number of 1 Bits

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

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

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

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

随机推荐

  1. 面向对象CSS (OOCSS)

    新版 OOCSS 请关注 http://www.oocss.cc/ 时下流行面向对象,那么有没有可能把样式表也面向对象一下呢,将现在的CSS(Cascading Style Sheets层叠样式表)进 ...

  2. HttpClient get返回String类型 JAVA

    public static String httpGet(String url) { // get请求返回结果 String strResult = ""; try { Defau ...

  3. Char Tools,方便的字符编码转换小工具

    工作关系,常有字符编码转换方面的需要,写了这个小工具 Char Tools是一款方便的字符编码转换小工具,基于.Net Framework 2.0 Winform开发 主要功能 URL编码:URLEn ...

  4. web笔记

    application: 在tomcat启动过程,会将所有的应用加载进来,会为每一个应用创建一个application对象.这个对象是唯一.但是所有的web应用是互不影响的. like模糊查询 重定向 ...

  5. VS2012发布网站IIS配置

    首先要配置好下面步骤 然后 把图上勾选的都勾选 最后一步 那IIS就配置好了,怎么添加发布呢打开IIS管理器 然后带点击网站添加网站 ,在这之前首先要在磁盘里新建一个文件夹,把项目复制过去,网站随便命 ...

  6. asp.net web编程开发将model键值对化

    关键字:model属性,反射 正文         model是数据库的映射,在.net web开发中,作为程序的最底层.web开发的一切都是基于数据库的,分了层之后,就基于model了. 为什么要将 ...

  7. Oracle Enterprise Manager快速重建

    我们在使用Oracle时, 可以利用Oracle自带的EM(Enterprise Manager)来更方便的管理我们的数据库.但是有时候我们的em却有时候无法连接,造成这个问题的原因有好多,例如没有正 ...

  8. CSS学习笔记总结和技巧

    跟叶老师说项目,他叫我写一个静态首页,看起来挺简单的,但是下手才发现在真的不会怎么下手啊,什么模型啊模块啊都不懂,写毛线啊!! 如图:页面下拉还有侧栏,中间内容等. 可是答应跟老师做了,不能怂啊,于是 ...

  9. 指针 v.s. 引用

    对C++的指针总觉得和引用差不多,其实还是挺有差别的. 程序 先看一个小程序: ; int& ra = a; int* pa = &ra; printf("&a = ...

  10. PHP实现对MongoDB的基础操作

    PHP扩展                                                                                      PHP5.2.PH ...