We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent c120238 commit 0a9444cCopy full SHA for 0a9444c
1 file changed
counting-bits/hjeomdev.java
@@ -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