Skip to content

Commit 02b7a35

Browse files
committed
add counting bits solution
1 parent e003d09 commit 02b7a35

File tree

1 file changed

+11
-0
lines changed

1 file changed

+11
-0
lines changed

counting-bits/Tessa1217.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,14 @@
11
class Solution {
2+
3+
// 시간복잡도: O(n)
4+
public int[] countBits(int n) {
5+
int[] bitsArray = new int[n + 1];
6+
for (int i = 1; i <= n; i++) {
7+
// Shift 연산자 사용
8+
// i&1 => 마지막 비트가 0인지 1인지 확인
9+
bitsArray[i] = bitsArray[i >> 1] + (i & 1);
10+
}
11+
return bitsArray;
12+
}
213
}
314

0 commit comments

Comments
 (0)