二进制数字中1的个数

题目

https://leetcode-cn.com/problems/er-jin-zhi-zhong-1de-ge-shu-lcof

解法

1
2
3
4
5
6
7
8
9
10
11
12
public class Solution {
// you need to treat n as an unsigned value
public int hammingWeight(int n) {

int res = 0;
while (n != 0) {
res++;
n = n&(n-1);
}
return res;
}
}
Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×