-
-
Notifications
You must be signed in to change notification settings - Fork 291
Expand file tree
/
Copy path338.java
More file actions
40 lines (36 loc) · 1.14 KB
/
338.java
File metadata and controls
40 lines (36 loc) · 1.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
__________________________________________________________________________________________________
sample 1 ms submission
class Solution {
public int[] countBits(int num) {
int[] bits = new int[num+1];
bits[0] = 0;
if(num < 1) {
return bits;
}
bits[1] = 1;
for(int i = 2; i <= num; ++i) {
bits[i] = (i & 0x1) + bits[i >>> 1];
}
return bits;
}
}
__________________________________________________________________________________________________
sample 35608 kb submission
class Solution {
public int[] countBits(int num) {
int[] res = new int[num+1];
for(int i = 0; i <= num; i++){
int cnt = 0;
String str = Integer.toBinaryString(i);
for(int j = 0; j < str.length(); j++){
char c = str.charAt(j);
char one = '1';
if(c == one)
cnt++;
}
res[i] = cnt;
}
return res;
}
}
__________________________________________________________________________________________________