Skip to content

Commit 0a9444c

Browse files
committed
Counting Bits
1 parent c120238 commit 0a9444c

1 file changed

Lines changed: 18 additions & 0 deletions

File tree

counting-bits/hjeomdev.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution {
2+
public int[] countBits(int n) {
3+
// 0 ~ n 까지 2진수를 구하고, 그 수의 모든 자릿값의 합을 구해야함
4+
5+
int[] result = new int[n + 1];
6+
7+
for (int i = 0; i <= n; i++) {
8+
String binaryString = Integer.toBinaryString(i);
9+
int sum = 0;
10+
for (int j = 0 ; j < binaryString.length(); j++) {
11+
sum += binaryString.charAt(j) - '0';
12+
}
13+
result[i] = sum;
14+
}
15+
16+
return result;
17+
}
18+
}

0 commit comments

Comments
 (0)