File tree Expand file tree Collapse file tree 1 file changed +33
-0
lines changed
Expand file tree Collapse file tree 1 file changed +33
-0
lines changed Original file line number Diff line number Diff line change 1+ """
2+ Constraints:
3+ - 0 <= n <= 10^5
14
5+ Time Complexity: O(n log n)
6+ - ์ธ๋ถ ๋ฃจํ: O(n) (0๋ถํฐ n๊น์ง ๋ฐ๋ณต)
7+ - hammingWeight ํจ์: O(log n)
8+ - ์ด ์๊ฐ ๋ณต์ก๋: O(n) * O(log n) = O(n log n)
9+
10+ Space Complexity: O(n)
11+ - ๊ฒฐ๊ณผ๋ฅผ ์ ์ฅํ๊ธฐ ์ํ ๊ธธ์ด n+1์ ๋ฐฐ์ด ํ์
12+
13+ ํ์ด๋ฐฉ๋ฒ:
14+ 1. ๊ธธ์ด๊ฐ n+1์ธ ans ๋ฐฐ์ด์ ์์ฑ
15+ 2. 0๋ถํฐ n๊น์ง์ ๊ฐ ์ซ์์ ๋ํด:
16+ - hammingWeight ํจ์๋ฅผ ์ฌ์ฉํ์ฌ ์ด์ง ํํ์์ 1์ ๊ฐ์๋ฅผ ๊ณ์ฐ
17+ - ๊ฒฐ๊ณผ๋ฅผ ans[i]์ ์ ์ฅ
18+ 3. ans ๋ฐฐ์ด ๋ฐํ
19+ """
20+ class Solution :
21+ def countBits (self , n : int ) -> List [int ]:
22+ ans = [0 ] * (n + 1 )
23+
24+ for i in range (n + 1 ):
25+ ans [i ] = self .hammingWeight (i )
26+ return ans
27+
28+
29+ def hammingWeight (self , n : int ) -> int :
30+ count = 0
31+ while n :
32+ count += n & 1
33+ n >>= 1
34+ return count
You canโt perform that action at this time.
0 commit comments