-
Notifications
You must be signed in to change notification settings - Fork 137
Expand file tree
/
Copy pathlc338.java
More file actions
31 lines (31 loc) · 857 Bytes
/
lc338.java
File metadata and controls
31 lines (31 loc) · 857 Bytes
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
package code;
/*
* 338. Counting Bits
* 题意:0~n数字上1的个数
* 难度:Medium
* 分类:Dynamic Programming, Bit Maniputation
* 思路:把前几个数的二进制写出来,就很容易看出来dp公式,前边的值+1即可
* Tips:注意细节,边界情况
*/
public class lc338 {
public static void main(String[] args) {
int[] res = countBits(0);
for (int i = 0; i < res.length ; i++) {
System.out.print(res[i]);
System.out.print(" ");
}
}
public static int[] countBits(int num) {
int[] dp = new int[num+1];
int i = 1;
while(i<=num) {
dp[i] = 1;
int max = i;
for (int j = 0; j<max && i<=num ; j++) {
dp[i] = 1 + dp[j];
i++;
}
}
return dp;
}
}