public class Solution {
// you need to treat n as an unsigned value
public int hammingWeight(int n) {
int count = 0;
while(n!=0){
n&=n-1;count++;
}
return count;
}
}
不多说了,这就是之前的那个计算数字二进制数中1的个数的(转换为在n!=0的情况下,n=n&(n-1)的次数。