-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCountingBits.java
More file actions
38 lines (32 loc) · 842 Bytes
/
CountingBits.java
File metadata and controls
38 lines (32 loc) · 842 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
32
33
34
35
36
37
38
package leetcode;
/**
* CountingBits
* https://leetcode-cn.com/problems/counting-bits
* 338. 比特位计数
* https://leetcode-cn.com/problems/counting-bits/solution/dong-tai-gui-hua-zhuan-yi-han-shu-she-ji-2dlf/
*
* @since 2021-03-03
*/
public class CountingBits {
// 输入: 2
// 输出: [0,1,1]
//
// 输入: 5
// 输出: [0,1,1,2,1,2]
public static void main(String[] args) {
CountingBits sol = new CountingBits();
int[] res = sol.countBits(20);
for (int value : res) {
System.out.print(value + ", ");
}
System.out.println();
}
public int[] countBits(int num) {
int[] bits = new int[num + 1];
bits[0] = 0;
for (int i = 0; i <= num; i++) {
bits[i] = bits[i >> 1] + (i & 1);
}
return bits;
}
}