我的解题:
1.将输入的整数逐位与1相与
int hammingWeight(uint32_t n) {
int count=0;
while(n){
for(int i=0;i<32;i++){
if(n&(1<<i)) count++;
}
}
return count;
}
};
2.n&(n-1)可以消除n最低位的1
class Solution {
public:
int hammingWeight(uint32_t n) {
int count=0;
while(n){
n&=n-1;
count++;
}
return count;
}
};