Skip to content

Commit 8d181d0

Browse files
committed
feat: counting-bits
1 parent 246ac78 commit 8d181d0

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

counting-bits/minji-go.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
* <a href="https://leetcode.com/problems/counting-bits/">week14-1. counting-bits</a>
3+
* <li>Description: 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</li>
4+
* <li>Topics: Dynamic Programming, Bit Manipulation</li>
5+
* <li>Time Complexity: O(N), Runtime 2ms </li>
6+
* <li>Space Complexity: O(N), Memory 46.64MB </li>
7+
*/
8+
class Solution {
9+
public int[] countBits(int n) {
10+
int[] count = new int[n + 1];
11+
12+
int offset = 1;
13+
for (int i = 1; i <= n; i++) {
14+
if (i == offset * 2) offset *= 2;
15+
count[i] = count[i - offset] + 1;
16+
}
17+
18+
return count;
19+
}
20+
}

0 commit comments

Comments
 (0)