Skip to content

Commit 96e85ba

Browse files
committed
rivkode counting bits
1 parent 459ae30 commit 96e85ba

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

โ€Žcounting-bits/rivkode.javaโ€Ž

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
ย (0)