题解 | #二进制中1的个数#
二进制中1的个数
https://www.nowcoder.com/practice/8ee967e43c2c4ec193b040ea7fbb10b8
class Solution {
public:
int NumberOf1(int n) {
bool b=false;
if(n<0)
{
n=-n-1;
b=true;
}
int ans=0;
while(n!=0)
{
if(n%2==1)
{
ans++;
}
n=n>>1;
}
if(b)
{
ans=32-ans;
}
return ans;
}
};
智元机器人成长空间 174人发布
