Fork me on GitHub

二进制中1的个数

题目描述

输入一个整数,输出该数二进制表示中1的个数。其中负数用补码表示。

解决方案

1
2
3
4
5
6
7
8
9
10
11
12
class Solution {
public:
int NumberOf1(int n) {
int count = 0;
while (n)
{
count++;
n = n&(n-1);
}
return count;
}
};

本文标题:二进制中1的个数

文章作者:LiuXiaoKun

发布时间:2019年03月20日 - 20:03

最后更新:2019年03月20日 - 20:03

原始链接:https://LiuZiQiao.github.io/2019/03/20/二进制中1的个数/

许可协议: 署名-非商业性使用-禁止演绎 4.0 国际 转载请保留原文链接及作者。

0%