【位运算专题】剑指 Offer 15. 二进制中1的个数

本问题对应课程链接:剑指offer刷题第四期

要求:

1、请打卡你的解题代码,如果能简单说一下思路则简单说一下思路。
2、给出你代码所用的时间复杂度和空间复杂度

共以下 21 个回答

  • hi 普通 2023年8月16日 下午9:04
    1. 每次利用公式n & (n – 1) 消掉最右边的1 ,直至n 等于0,消了几次就证明有多少个1
    class Solution {
    public:
        int hammingWeight(uint32_t n) {
            int res = 0;
            while(n){
                n &= n - 1; 
                res++; 
            }
            return res;
        }
    };
    
    0 赞同 0 条回复
  • doordie. 普通 2023年8月15日 上午9:35

    逐位判断

    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;
        }
    }
    
    0 赞同 0 条回复
  • 艺 普通 2023年8月3日 下午11:01

    “`c++
    //公式
    class Solution {
    public:
    int hammingWeight(uint32_t n) {
    int count=0;
    while(n)
    {
    n=n&(n-1);
    count++;
    }
    return count;
    }
    };

    0 赞同 0 条回复
  •  普通 2023年8月1日 下午10:15

    c++ 简单的位运算
    class Solution {
    public:
    int hammingWeight(uint32_t n) {
    int s=0;
    while(n)
    {
    s+=n&1;
    n>>=1;
    }
    return s;
    }

    };

    0 赞同 0 条回复
  • CaCO3 永久会员 2023年8月1日 下午7:41

    ”’java
    public class Solution {
    // you need to treat n as an unsigned value
    public int hammingWeight(int n) {
    int ret = 0;
    while(n != 0){
    n &= n-1;//将二进制位中最低位的1变为0之后的结果
    ret++;

        }
        return ret;
    }
    

    }
    ”’
    时间复杂度O(logn)
    空间复杂度O(1)

    0 赞同 0 条回复
  • 北辰 普通 2023年7月31日 下午5:21
    public static int hammingWeight1(int n) {
            int ret = 0;
            while(n!=0) {
                n&=(n-1);
                ret++;
            }
            return ret;
        }
    

    时间复杂度:O(logn)
    空间复杂度:O(1)

    0 赞同 0 条回复
  • Mby 普通 2023年7月29日 下午9:28
    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 ++;
            }
            return sum;
    
        }
    }
    
    0 赞同 0 条回复
  • 今天你刷题了嘛 普通 2023年7月27日 下午11:18
    public class Solution {
        // you need to treat n as an unsigned value
        public int hammingWeight(int n) {
            int count=0;
            for(int i=0;i<32;i++){
                int temp = 1&n;
                if(temp==1) count++;
                n = n>>1;
            }
            return count;
        }
    }
    

    时间复杂度:o(1)

    0 赞同 0 条回复
  • NullPointerException 普通 2023年7月27日 上午11:26

    public class Solution {
    // you need to treat n as an unsigned value
    public int hammingWeight(int n) {
    int count = 0;
    while(n!=0)
    {
    if((n&1)1)//预算付优先级
    {
    count+=(n&1);
    }
    n>>>=1;
    }
    return count;
    }
    }

    0 赞同 0 条回复
  • 土豆芋泥包_ 普通 2023年7月26日 下午9:15

    代码

    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++;
            }
            return sum;
        }
    }
    
    0 赞同 0 条回复
  • CKX 普通 2023年7月26日 下午4:25
    func hammingWeight(num uint32) int {
        res := 0
        for num != 0 {
            num = num & (num - 1)
            res++
        }
        return res
    }
    
    • 时间O(logn)
    • 空间O(1)
    0 赞同 0 条回复
  • Marshan 普通 2023年7月26日 下午3:40

    不难

    class Solution {
    public:
        int hammingWeight(uint32_t n) {
            int res = 0;
            while(n != 0){
                if((n & 1) == 1){
                    res++;
                }
                n >>= 1;
            }
            return res;
        }
    };
    
    0 赞同 0 条回复
  • mpweixin用户 普通 2023年7月25日 下午3:47
    public int hammingWeight(int n) {
                //输入:n = 11 (控制台输入 00000000000000000000000000001011)
                int index = 1;
                int count = 0;
                for (int i = 0; i < 32; i++) {
                    if ((index & n) != 0) {
                        count++;
                    }
                    index = index << 1;
                }
                return count;
            }
    
    0 赞同 0 条回复
  • 人生何处不青山 普通 2023年7月25日 上午11:26

    JS简单题

    /**
     * @param {number} n - a positive integer
     * @return {number}
     */
    var hammingWeight = function(n) {
        let res = 0;
        while(n) {
            res += n & 1;
            n >>>= 1;
        }
        return res;
    };
    
    0 赞同 0 条回复
  • 刹那芳间 普通 2023年7月25日 上午10:40
    public class Solution {
        // you need to treat n as an unsigned value
        public int hammingWeight(int n) {
             int count = 0;
             while(n!=0){
                 // 每次消除最右边的1
                 n = n&(n-1);
                 count++;
             }
             return count;
        }
    }
    
    0 赞同 0 条回复
  • 阿尚 普通 2023年7月24日 下午11:41
    public class Solution {
        // you need to treat n as an unsigned value
        public int hammingWeight(int n) {
            int count = 0;
            while(n != 0){
                int lastOne = n & ((~n) + 1);//去二进制的最后一位1
                n ^= lastOne;
                count++;
    
            }
    
            return count;
        }
    }
    
    0 赞同 0 条回复
  • #00456B 普通 2023年7月24日 下午6:49
    class Solution {
    public:
        //用n&(n-1)可以实现消除最右的1
        //   n    = 1000100
        //  n-1   = 1000011
        //n&(n-1) = 1000000
        int hammingWeight(uint32_t n) {
            int ans = 0;
            while(n != 0){
                n = n & (n - 1);
                ans++;
            }
            return ans;
        }
    };
    
    class Solution {
    public:
        int hammingWeight(uint32_t n) {
            int ans=0;
            //用只有一个1的数来遍历32次,记录有几个1
            for(int i=0;i<32;i++){
                if(n & (1<<i)){
                    ans++;
                }
            }
            return ans;
        }
    };
    
    0 赞同 0 条回复
  • 长河呼叫黄江 普通 2023年7月24日 下午6:14

    7.24
    Day16:下面是代码

    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;
        }
    }
    
    
    
    0 赞同 0 条回复
  • Luminous 普通 2023年7月24日 下午5:14

    以下是代码:

    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++;
            }
            return sum;
        }
    }
    
    0 赞同 0 条回复
  • AD钙奶 普通 2023年7月24日 下午3:00

    将n往右移来比较,会超时。用另一个数不停左移,与n比较就不会超时。有点奇妙!

    public class Solution {
        // you need to treat n as an unsigned value
        public int hammingWeight(int n) {
            int cnt = 0;
            while(n != 0) {
                if(n & 1 == 1) {
                    cnt ++;
                }
                n >>= 1;
            }
            return cnt;
        }
    }
    
    0 赞同 0 条回复
  • June 普通 2023年7月24日 下午2:47
    func hammingWeight(num uint32) int {
        var cnt int = 0
        for num != 0 {
            tmp := num & 1
            cnt = int(tmp) + cnt
            num = num >> 1
        }
        return cnt
    }
    

    时间复杂度 On 空间复杂度 O1

    0 赞同 0 条回复

# 回答此问题

后才能回答