File tree Expand file tree Collapse file tree 1 file changed +26
-0
lines changed Expand file tree Collapse file tree 1 file changed +26
-0
lines changed Original file line number Diff line number Diff line change 1+ class SolutionCountingBits {
2+ public int [] countBits (int n ) {
3+ // 0 ~ n κΉμ§μ μλ₯Ό μ΄μ§μλ‘ λ³ννλ€μ, 1μ κ°μλ₯Ό μΉ΄μ΄νΈν΄ λ°°μ΄λ‘ λ°ν
4+ // νμ/μ§μ μ¬λΆλ₯Ό λλ μ 1μ κ°μλ₯Ό ꡬν¨
5+ // νμ: μ΄μ κ° + 1, μ§μ: i / 2μ 1μ κ°μμ κ°μ κ°
6+ // μκ°λ³΅μ‘λ: O(N), 곡κ°λ³΅μ‘λ: O(N)
7+
8+ int [] countingBits = new int [n + 1 ];
9+ countingBits [0 ] = 0 ;
10+
11+ for (int i =1 ; i <=n ; i ++) {
12+ if (isOddNumber (i )) {
13+ countingBits [i ] = countingBits [i - 1 ] + 1 ;
14+ } else {
15+ countingBits [i ] = countingBits [i / 2 ];
16+ }
17+ }
18+
19+ return countingBits ;
20+ }
21+
22+ // μκ°λ³΅μ‘λ: O(1)
23+ private boolean isOddNumber (int n ) {
24+ return n % 2 == 1 ;
25+ }
26+ }
You canβt perform that action at this time.
0 commit comments