forked from DaleStudy/leetcode-study
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjaejeong1.java
More file actions
26 lines (22 loc) Β· 737 Bytes
/
jaejeong1.java
File metadata and controls
26 lines (22 loc) Β· 737 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
class SolutionCountingBits {
public int[] countBits(int n) {
// 0 ~ n κΉμ§μ μλ₯Ό μ΄μ§μλ‘ λ³ννλ€μ, 1μ κ°μλ₯Ό μΉ΄μ΄νΈν΄ λ°°μ΄λ‘ λ°ν
// νμ/μ§μ μ¬λΆλ₯Ό λλ μ 1μ κ°μλ₯Ό ꡬν¨
// νμ: μ΄μ κ° + 1, μ§μ: i / 2μ 1μ κ°μμ κ°μ κ°
// μκ°λ³΅μ‘λ: O(N), 곡κ°λ³΅μ‘λ: O(N)
int[] countingBits = new int[n + 1];
countingBits[0] = 0;
for (int i=1; i<=n; i++) {
if (isOddNumber(i)) {
countingBits[i] = countingBits[i - 1] + 1;
} else {
countingBits[i] = countingBits[i / 2];
}
}
return countingBits;
}
// μκ°λ³΅μ‘λ: O(1)
private boolean isOddNumber(int n) {
return n % 2 == 1;
}
}