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;
}
}