0
点赞
收藏
分享

微信扫一扫

2022-02-12(338. 比特位计数)

雅典娜的棒槌 2022-02-14 阅读 64
java
class Solution {
    public int[] countBits(int n) {
        int[] result=new int[n+1];
        for(int i=0;i<=n;++i){
            result[i]=oneCount(i);
        }
        return result;
    }
    public int oneCount(int x){
        int count=0;
        while(x!=0){
            x=x&(x-1);
            ++count;
        }
        return count;
    }
}
举报

相关推荐

0 条评论