public class Solution {
// you need to treat n as an unsigned value
public int hammingWeight(int n) {
int ans = 0;
while(n!=0) {
if ((n & 1) 1) {
ans++;
}
n = n >>> 1;
}
return ans;
}
}
public class Solution {
public int hammingWeight(int n) {
int sum = 0;
while(n != 0){
// n & (n – 1)可以消除n最右边的一个 1(二进制表示)
n = n & (n – 1);
sum ++;
}
return sum;
}
}
public class Solution {
// you need to treat n as an unsigned value
public int hammingWeight(int n) {
int cnt = 0;
for (int i = 0; i < 32; i++) {
if ((n & 1) == 1) {
cnt++;
}
n = n >> 1;
}
return cnt;
}
}
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&1;
n >>>= 1;
}
return res;
}
}
public class Solution {
// you need to treat n as an unsigned value
public int hammingWeight(int n) {
int sum=0;
while(n!=0){
n=n&(n-1);
sum++;
}
//超时
// while(n!=0){
// if((n & 1) ==1){
// sum++;
// }
// n=n>>1;
// }
return sum;
}
}
public class Solution {
// you need to treat n as an unsigned value
public int hammingWeight(int n) {
int ans = 0;
while(n != 0){
ans += (n & 1);
n >>>= 1;
}
return ans;
}
}//方法一
//方法二
public class Solution {
// you need to treat n as an unsigned value
public int hammingWeight(int n) {
int ans = 0;
while(n != 0){
ans++;
n &= n-1;
}
return ans;
}
}
不断让当前的 n 与 n−1做与运算,直到 n 变为 0 即可。n&(n-1)可以把n最右边的一个1变为0,所以一直这样下去,每进行一次与运算count就加1,直到n为0,最后count就是n的二进制中1的个数。
“`java
public int hammingWeight(int n) {
int sum = 0;
while(n != 0){
// n & (n – 1)可以消除n最右边的一个 1(二进制表示)
n = n & (n – 1);
sum ++;
}
return sum;
}
“`java
时间复杂度:O(logn)
空间复杂度:O(1)
var hammingWeight = function(n) {
let res = 0
while(n){
res += n%2==1
n = Math.floor(n/2)
}
return res
};
var hammingWeight = function(n) {
let res = 0
while(n){
n = n & (n-1)
res ++
}
return res
};
java
public class Solution {
// you need to treat n as an unsigned value
public int hammingWeight(int n) {
int ret = 0;
for(int i = 0; i < 32; i++){
if((n & (1 << i)) != 0){
ret++;
}
}
return ret;
}
}
提交
public class Solution {
// you need to treat n as an unsigned value
public int hammingWeight(int n) {
int count = 0;
while(n!=0){
count++;
n = n&(n-1);
}
return count;
}
}
思路:n&(n-1)可以把n最右边的一个1变为0,所以一直这样下去,每进行一次与运算count就加1,直到n为0,最后count就是n的二进制中1的个数。
共以下 20 个回答
public class Solution {
// you need to treat n as an unsigned value
public int hammingWeight(int n) {
int ans = 0;
while(n!=0) {
if ((n & 1) 1) {
ans++;
}
n = n >>> 1;
}
return ans;
}
}
无符号右移
public class Solution {
public int hammingWeight(int n) {
int sum = 0;
while(n != 0){
// n & (n – 1)可以消除n最右边的一个 1(二进制表示)
n = n & (n – 1);
sum ++;
}
return sum;
}
}
位与运算
时间复杂度O(1)
空间复杂度O(1)
class Solution {
public:
int hammingWeight(uint32_t n) {
int ret = 0;
while(n!=0){
n = n&(n-1);//运行一次消除一个1
ret++;
}
return ret;
}
};
公式n&(n-1)用于消除最后面的1,时间复杂度O(logn),空间复杂度O(1)
原理:每次右移一个位后和1与,只有最后一位是1才能满足条件
时间复杂度:O(logn)
空间复杂度:O(1)
打卡
public class Solution {
// you need to treat n as an unsigned value
public int hammingWeight(int n) {
int ans = 0;
while(n != 0){
ans += (n & 1);
n >>>= 1;
}
return ans;
}
}//方法一
//方法二
public class Solution {
// you need to treat n as an unsigned value
public int hammingWeight(int n) {
int ans = 0;
while(n != 0){
ans++;
n &= n-1;
}
return ans;
}
}
class Solution {
public:
int hammingWeight(uint32_t n) {
uint32_t res = 0;
while(n != 0){
if(n & 1) res++;
n = n >> 1;
}
return res;
}
};
时间复杂度:O(log2N),空间复杂度O(1)
不断让当前的 n 与 n−1做与运算,直到 n 变为 0 即可。n&(n-1)可以把n最右边的一个1变为0,所以一直这样下去,每进行一次与运算count就加1,直到n为0,最后count就是n的二进制中1的个数。
“`java
public int hammingWeight(int n) {
int sum = 0;
while(n != 0){
// n & (n – 1)可以消除n最右边的一个 1(二进制表示)
n = n & (n – 1);
sum ++;
}
return sum;
}
“`java
时间复杂度:O(logn)
空间复杂度:O(1)
位运算
java
public class Solution {
// you need to treat n as an unsigned value
public int hammingWeight(int n) {
int ret = 0;
for(int i = 0; i < 32; i++){
if((n & (1 << i)) != 0){
ret++;
}
}
return ret;
}
}
提交
思路:每次右移一位,判断最后一位是否为1
时间复杂度O(n)
空间复杂度O(1)
“`C++
class Solution {
public:
int hammingWeight(uint32_t n)
{
int res = 0;
while(n != 0)
{
res += n & 1;
n >>= 1;
}
return res;
}
};
“`
class Solution {
public:
int hammingWeight(uint32_t n) {
int res = 0;
while(n){
n &= n-1;
res ++;
}
return res;
}
};
空间复杂度与1的个数线性相关
public int hammingWeight(int n) {
int count=0;
while(n!=0){
count+=n&1;
n=n>>>1;
}
return count;
}
思路:位运算