JZ11 二进制中1的个数

本文最后更新于:2022年4月9日 中午

image-20211006104417837

Solution

  • 位运算
1
2
3
4
5
6
7
8
9
10
11
class Solution {
public:
int NumberOf1(int n) {
int res = 0;
while (n != 0) {
res++;
n = n & (n-1);
}
return res;
}
};

本博客所有文章除特别声明外,均采用 CC BY-SA 4.0 协议 ,转载请注明出处!