Problem: 191. 位1的个数
文章目录
思路
复杂度

Code
Java
public class Solution {
// you need to treat n as an unsigned value
public int hammingWeight(int n)
{
int res = 0;
while (n != 0)
{
res += 1;
n &= n - 1;// 把最后一个出现的 1 改为 0,和 lowbit 有异曲同工之妙
}
return res;
}
}