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 459ae30 commit 96e85baCopy full SHA for 96e85ba
โcounting-bits/rivkode.javaโ
@@ -0,0 +1,27 @@
1
+/*
2
+์ ๋ ฅ๋ฐ์ n์ ๋ํด toBinaryString() ์ ์ฌ์ฉํ์ฌ binaryString ๊ฐ์ ์์๋ธ๋ค charAt()์ผ๋ก ๊ฐ ์ธ๋ฑ์ค๋ณ ์ ๊ทผํ์ฌ 1์ ๊ฐ์๋ฅผ ์ฐพ์๋ด๋ ๋ฐฉ์์ด๋ค.
3
+ */
4
+
5
+class Solution {
6
+ public int[] countBits(int n) {
7
+ int[] answer = new int[n + 1];
8
+ for (int i=0; i<n+1; i++) {
9
+ String bit = Integer.toBinaryString(i);
10
+ System.out.println(bit);
11
+ int cnt = 0;
12
+ for (int j=0; j < bit.length(); j++) {
13
+ char c = bit.charAt(j);
14
+ int num = c-'0';
15
+ if (num == 1) {
16
+ cnt += 1;
17
+ }
18
19
20
+ answer[i] = cnt;
21
22
23
+ return answer;
24
25
+}
26
27
0 commit comments