题目
二进制中1的个数
C++代码
class Solution {
public:
int hammingWeight(uint32_t n){
int res = 0;
while(n){
res++;
n = n & (n - 1);
}
return res;
}
};
Java代码
public class Solution {
// you need to treat n as an unsigned value
public int hammingWeight(int n){
int res = 0;
while(n != 0){
n = n & (n - 1);
res++;
}
return res;
}
}
联系方式
如果有任何问题可以邮箱联系我:raymondlam1@yeah.net