0
点赞
收藏
分享

微信扫一扫

剑指 Offer 15. 二进制中1的个数

Sky飞羽 2022-01-06 阅读 33
leetcode

题目

二进制中1的个数

C++代码

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

Java代码

public class Solution {
    // you need to treat n as an unsigned value
    public int hammingWeight(int n){
        int res = 0;
        while(n != 0){
            n = n & (n - 1);
            res++;
        }
        return res;
    }
}

联系方式

如果有任何问题可以邮箱联系我:raymondlam1@yeah.net

举报

相关推荐

0 条评论