负数补码不能用辗转相除。按位减一相与,一次可以消掉右侧的一个1
class Solution {
public:
int NumberOf1(int n) {
int ans=0;
while(n!=0){
ans++;
n=n&(n-1);
}
return ans;
}
};
class Solution {
public:
int NumberOf1(int n) {
int ans=0;
while(n!=0){
ans++;
n=n&(n-1);
}
return ans;
}
};