此题看书上笔记
最优解
class Solution {
public:
int hammingWeight(uint32_t n) {
int count=0;
while(n){
++count;
n=n&(n-1);
}
return count;
}
};
非最优解:
class Solution {
public:
int hammingWeight(uint32_t n) {
int count=0;int flag=1;
while(flag){
if(n&flag){
++count;
flag=flag<<1;
}
}
return count;
}
};
这个甚至跑不出来 而且代码感觉有问题