-
Notifications
You must be signed in to change notification settings - Fork 0
/
CountingBits.java
61 lines (51 loc) · 1.43 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
61
package bit_manipulation.easy;
import java.util.Arrays;
/***
* Problem 338 in Leetcode: https://leetcode.com/problems/counting-bits/
*
* Given an integer n, return an array ans of length n + 1 such that for each i (0 <= i <= n),
* ans[i] is the number of 1's in the binary representation of i.
*
* Example 1:
* Input: n = 2
* Output: [0,1,1]
*
* Example 2:
* Input: n = 5
* Output: [0,1,1,2,1,2]
*/
public class CountingBits {
public static void main(String[] args) {
int num = 5;
System.out.println("Brute Force: " + Arrays.toString(countBitsBruteForce(num)));
System.out.println("Dynamic Programming: " + Arrays.toString(countBitsDP(num)));
}
private static int[] countBitsBruteForce(int num) {
if (num == 0) {
return new int[]{0};
}
int[] result = new int[num + 1];
result[0] = 0;
for (int i = 1; i <= num; i++) {
int count = 0;
int n = i;
while (n != 0) {
count++;
n = (n & (n - 1));
}
result[i] = count;
}
return result;
}
private static int[] countBitsDP(int num) {
if (num == 0) {
return new int[]{0};
}
int[] result = new int[num + 1];
result[0] = 0;
for (int i = 1; i <= num; i++) {
result[i] = result[i >> 1] + (i % 2);
}
return result;
}
}