代码:
class Solution {
public int[] countBits(int n) {
int[] arr = new int[n+1];
for(int i=0;i<=n;i++){
int x = i;
int cnt = 0;
while(x!=0){
cnt+=x%2;
x/=2;
}
arr[i]=cnt;
}
return arr;
}
}