LeetCode 191. Number of 1 Bits Question
题意:给你一个整数,计算该整数的二进制形式里有多少个“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的更多相关文章
- Leetcode#191. Number of 1 Bits(位1的个数)
题目描述 编写一个函数,输入是一个无符号整数,返回其二进制表达式中数字位数为 '1' 的个数(也被称为汉明重量). 示例 : 输入: 11 输出: 3 解释: 整数 11 的二进制表示为 000000 ...
- 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 ...
- 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 ...
- [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 ...
- LeetCode 191 Number of 1 Bits
Problem: Write a function that takes an unsigned integer and returns the number of '1' bits it has ( ...
- 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 ...
- (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 ...
- 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 ...
- [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 ...
随机推荐
- BootStrap——模态框
模态框(Modal)是BootStrap中很棒的一个插件.可以去BootStrap菜鸟驿站里面看看. 模态框(Modal)是覆盖在父窗体上的子窗体.通常,目的是显示来自一个单独的源的内容,可以在不离开 ...
- PHP各版本之间差异
PHP5.3 __toString 魔术方法不再接受参数. 魔术方法 __get, __set, __isset, __unset, and __call 应该总是公共的(public)且不能是静态的 ...
- lesson2:java阻塞队列的demo及源码分析
本文向大家展示了java阻塞队列的使用场景.源码分析及特定场景下的使用方式.java的阻塞队列是jdk1.5之后在并发包中提供的一组队列,主要的使用场景是在需要使用生产者消费者模式时,用户不必再通过多 ...
- Tooltip(提示框)组件
一.加载方式 //class加载方式 <a href="http://www.ycku.com" title="这是一个提示信息!" class=&quo ...
- c#.net防止按F5刷新页面重复提交的方法
在网上购物的过程中,提交完一个页面后,如果此时按f5刷新,则会弹出一个提示:如果继续,则会重新发送提交我们刚才提交的内容,这个问题应该规避掉,不然总是重复提交付款,那可不是件好事. 在c#.net中的 ...
- 【nodejs学习】0.nodejs学习第一天
1.模块 大一点的程序都需要模块化,nodejs也不例外,代码放到不同的文件中,每一个文件就可以是一个模块,文件路径名就是一个模块名.每个模块中包含三个预先定义的变量: 1.require:用于在当前 ...
- SQL2008转SQL2005数据库经验
1.用SQL2008创建兼容2005的结构脚本. 2.在2005中生成数据库结构. 3.利用2005中的数据导入直接从源数据库中导入数据,此处注意自增长标识的选项需要添加,多表优化选项可以去掉,这里不 ...
- foreach遍历扩展(二)
一.前言 假设存在一个数组,其遍历模式是根据索引进行遍历的:又假设存在一个HashTable,其遍历模式是根据键值进行遍历的:无论哪种集合,如果它们的遍历没有一个共同的接口,那么在客户端进行调用的时候 ...
- linux下enum的使用
enum T { status1, status2, } Linux下: 1.做函数返回值时enum T f():不能写成T f(): 2.if(i == status1)不能写成 if(i == T ...
- MySQL 数据库操作命令汇总
此文全部都是基本的数据库语言 1.登陆到mysql >mysql -h hostname -u username -p 然后等待系统提示输入密码即可登陆.如果想在登陆的时候就选择好数据库,可以使 ...