forked from DaleStudy/leetcode-study
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtaekwon-dev.java
More file actions
19 lines (19 loc) Β· 795 Bytes
/
taekwon-dev.java
File metadata and controls
19 lines (19 loc) Β· 795 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
/**
* κ³΅κ° λ³΅μ‘λ: O(N)
* μκ° λ³΅μ‘λ: O(N)
* - λ§μ½ 1 ~ N κΉμ§ κ° μμ λν΄μ μ΄μ§μλ‘ λ³νν λ€ 1μ κ°μλ₯Ό μΉ΄μ΄νΈ νλ€λ©΄? - O(NlogN)
* Note:
* - a >> b: a μ μ΄μ§ ννμ μ€νμͺ½μΌλ‘ b λΉνΈλ§νΌ μ΄λ
* - i >> 1: a μ μ΄μ§ ννμμ κ°μ₯ μ€νμͺ½ νλκ° μλ¦Ό (iλ μμ λ²μλ₯Ό κ°μ§λ―λ‘, μΌμͺ½μ 0μΌλ‘ μ±μμ§)
* - a & b: AND μ°μ°
* - i & 1: μ΄μ κ²°κ³Ό κ°μ΄ λ©λͺ¨λμ΄ μμΌλ―λ‘, λ΄κ° κΆκΈν κ² κ°μ₯ λ§μ§λ§ μ리 μ κ°μ΄ 1μΈμ§ μ¬λΆ.
*/
class Solution {
public int[] countBits(int n) {
int[] result = new int[n + 1];
for (int i = 1; i <= n; i++) {
result[i] = result[i >> 1] + (i & 1);
}
return result;
}
}