-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathP0338_CountingBits.java
More file actions
56 lines (51 loc) · 1.92 KB
/
P0338_CountingBits.java
File metadata and controls
56 lines (51 loc) · 1.92 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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package yyl.leetcode.p03;
import yyl.leetcode.util.Assert;
/**
* <h3>比特位计数</h3> 给定一个非负整数 num。对于 0 ≤ i ≤ num 范围中的每个数字 i ,计算其二进制数中的 1 的数目并将它们作为数组返回。 <br>
*
* <pre>
* 示例 1:
* 输入: 2
* 输出: [0,1,1]
*
* 示例 2:
* 输入: 5
* 输出: [0,1,1,2,1,2]
* </pre>
*/
public class P0338_CountingBits {
public static void main(String[] args) {
Solution solution = new Solution();
Assert.assertEquals(new int[] { 0, 1, 1 }, solution.countBits(2));
Assert.assertEquals(new int[] { 0, 1, 1, 2, 1, 2 }, solution.countBits(5));
}
// 直接计算
// 最直观的方法是对从 000 到 num\textit{num}num 的每个数直接计算「一比特数」
// 时间复杂度:O(n*sizeof(integer))。
// 空间复杂度:O(1)。
static class Solution {
public int[] countBits(int num) {
int[] answer = new int[num + 1];
for (int i = 0; i <= num; i++) {
answer[i] = Integer.bitCount(i);
}
return answer;
}
}
// 动态规划——位运算
// 对于正整数 x ,将其二进制表示右移一位,等价于将其二进制表示的最低位去掉,得到的数是 ⌊x/2⌋。如果 bits[⌊x2⌋] 的值已知,则可以得到 bits[x] 的值:
// 如果 x 是偶数,则 bits[x]=bits[⌊x2⌋];
// 如果 x 是奇数,则 bits[x]=bits[⌊x2⌋]+1;
// 上述两种情况可以合并成:bits[x] 的值等于 bits[⌊x2⌋] 的值加上 x 除以 2 的余数。
// 时间复杂度:O(n)
// 空间复杂度:O(1)。
static class Solution1 {
public int[] countBits(int num) {
int[] answer = new int[num + 1];
for (int i = 1; i <= num; i++) {
answer[i] = answer[i >> 1] + (i & 1);
}
return answer;
}
}
}