【位运算】剑指 Offer 15. 二进制中1的个数( //这里不能用n>0 ??? while(n!=0){....} 为什么 )

题目:

请实现一个函数,输入一个整数,输出该数二进制表示中 1 的个数。例如,把 9 表示成二进制是 1001,有 2 位是 1。因此,如果输入 9,则该函数输出 2。

示例 1:

输入:00000000000000000000000000001011
输出:3
解释:输入的二进制串 00000000000000000000000000001011 中,共有三位为 '1'。

 

解答:

方法一,位运算

public class Solution {
    // you need to treat n as an unsigned value
    public int hammingWeight(int n) {
        int cnt = 0;

        //这里不能用n>0 ???
        while(n!=0){
            if((n&1) == 1){
                cnt++;
            }
            n = n>>>1;
        }

        return cnt;
    }
}

 方法二:

使用 n & (n-1)

 

 

public class Solution {
    public int hammingWeight(int n) {
        int res = 0;
        while(n != 0) {
            res++;
            n &= n - 1;
        }
        return res;
    }
}

 

posted @ 2020-10-19 22:43  3KBLACK  阅读(87)  评论(0)    收藏  举报