您的位置:首页 > 其它

191 Number of 1 Bits

2015-08-04 11:20 295 查看
/*

* 32位无符号数最大为2147483647

* Java中没有无符号类型,所以此题不能用n%2的方法解题

*/

public class Solution {

// you need to treat n as an unsigned value

public int hammingWeight(int n) {

int bits = 0;

int t = 0;

while(n!=0){

bits += (n&1);

n = n >>> 1;

}

return bits;

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: