-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathCountingBits.java
60 lines (58 loc) · 1.41 KB
/
CountingBits.java
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
57
58
59
60
package io.ziheng.bitmanipulation.leetcode;
import java.util.Arrays;
/**
* LeetCode 338. Counting Bits
* https://leetcode.com/problems/counting-bits/
*/
public class CountingBits {
// 主函数 -> 测试用例
public static void main(String[] args) {
CountingBits obj = new CountingBits();
System.out.println(
Arrays.toString(
obj.countBits(2)
)
);
System.out.println(
Arrays.toString(
obj.countBits(5)
)
);
}
/**
* 统计比特位 1 的数目
*
* 时间复杂度:O(n * sizeof(m))
* 空间辅助度:O(n)
*
* 1. 开结果数组 -> 容量 n + 1
* 2. 依次计算 [0... n] 数值的比特位 1 数目
* 3. 二进制计算方法 -> 对 2 取余
*
* @param num
* @return int[]
*/
public int[] countBits(int num) {
// 异常情况
if (num < 0) {
return new int[0];
}
int[] resultArray = new int[num + 1];
for (int i = 0; i <= num; i++) {
resultArray[i] = countNumBits(i);
}
return resultArray;
}
private int countNumBits(int num) {
int cnt = 0;
while (num > 0) {
int remainder = num % 2;
num /= 2;
if (remainder == 1) {
cnt++;
}
}
return cnt;
}
}
/* EOF */