剑指offer 前n个数二进制中1的个数
力扣题目链接
位运算
class Solution {
public int[] countBits(int n) {
int[] nums = new int[n+1];
for(int i=0;i<=n;++i){
for(int j=0;j<32;++j){
nums[i] += (i>>j) &1;
}
}
return nums;
}
}
本文来自博客园,作者:蹇爱黄,转载请注明原文链接:https://www.cnblogs.com/jianjiana/p/15861670.html