# leetcode之Counting Bits

mport java.util.Scanner;

public class Solution {
public static int[] countBits(int num) {
int[] sum = new int[num + 1];
for(int i = 0; i <= num; i++)
{
int count = 0;
int j = i;
while(j/2 >= 0 && j >0)
{
if(j != j/2*2)
count += 1;
j = j/2;
}
sum[i]=count;

}
return sum;

}
public static void main(String args[])
{
Scanner n = new Scanner(System.in);
int num = n.nextInt();
int k = Integer.SIZE;
int[] sum = new int[num + 1];
sum = countBits(num);
for(int i =0; i <= num; i ++)
System.out.print(sum[i]);

}

}

import java.util.Scanner;

public class Solution {
public static int[] countBits(int num) {
int[] sum = new int[num + 1];
int k  = 1;
if(num ==0)
sum[0] = 0;
else if(num ==1)
sum[1] = 1;

else
{
sum[0] = 0;
sum[1] = 1;
int i = (int) Math.pow(2, k);
for( ; i + 1< (i*2) && i<=num; i +=2)
{
if(i == num)
sum[i]=sum[i/2];
else
{
sum[i]=sum[i/2];
sum[i+1]=sum[i/2]+1;
}
}
}

return sum;

}
public static void main(String args[])
{
Scanner n = new Scanner(System.in);
int num = n.nextInt();
int k = Integer.SIZE;
int[] sum = new int[num + 1];
sum = countBits(num);
for(int i =0; i <= num; i ++)
System.out.print(sum[i]);

}

}

import java.util.Scanner;

public class Solution {
public static int[] countBits(int num) {
int[] sum = new int[num + 1];
sum[0] =0;
for(int i = 0; i <= num; i++)
if(i%2 == 0)
sum[i] = sum[i/2];
else
sum[i] = sum[i/2]+1;

return sum;

}
}

#### [leetcode] 338. Counting Bits

2016-03-18 11:57:14

#### [leetcode-338]Counting Bits(java)

2016-03-27 20:42:54

#### leetcode-338-Counting Bits

2016-03-19 11:09:40

#### LeetCode--338. Counting Bits(计算比特数)Python

2018-01-02 18:35:08

#### 338. Counting Bits [medium] (Python)

2016-07-29 13:27:16

#### LeetCode 338. Counting Bits C语言

2016-04-28 20:58:51

#### 338. Counting Bits 数字的二进制中1的个数

2016-05-07 21:31:47

#### LeetCode(190) Reverse Bits

2015-03-22 12:46:11

#### 【LeetCode-190】 Reverse Bits(C++)

2016-05-20 23:11:28

#### LeetCode 第 338 题 (Counting Bits)

2016-04-19 09:30:21