File tree Expand file tree Collapse file tree 1 file changed +39
-2
lines changed Expand file tree Collapse file tree 1 file changed +39
-2
lines changed Original file line number Diff line number Diff line change 1
1
"""
2
2
[๋ฌธ์ ํ์ด]
3
3
# Inputs
4
-
4
+ n: int
5
5
# Outputs
6
6
7
+ ans: arr
8
+ ๊ธธ์ด: n + 1
9
+
10
+ ans๋ด ๊ฐ ์์๋ค : ans[i]: i์ ์ด์ง๋ฒ์์ 1์ ๊ฐ์
11
+
7
12
# Constraints
13
+ 0 <= n <= 10^5
8
14
9
15
# Ideas
16
+ 2์ค for๋ฌธ ์ด๋ฉด ์๋จ
10
17
11
18
[ํ๊ณ ]
12
-
19
+ dp๋ฅผ ํ์ฉํ ํ์ด๋ ๊ฐ์ด ์์๋์
13
20
"""
14
21
22
+ # ๋ด ํ์ด
23
+ class Solution :
24
+ def countBits (self , n : int ) -> List [int ]:
25
+
26
+ ans = []
27
+
28
+ for i in range (n + 1 ):
29
+ if i == 0 or i == 1 :
30
+ ans .append (i )
31
+ continue
32
+
33
+ num = i
34
+ cnt = 0
35
+ while num > 0 :
36
+ num , n = num // 2 , num % 2
37
+ if n == 1 :
38
+ cnt += 1
39
+
40
+ ans .append (cnt )
41
+
42
+ return ans
43
+
44
+ # ํด์ค
45
+ class Solution :
46
+ def countBits (self , n : int ) -> List [int ]:
47
+ dp = [0 ] * (n + 1 )
48
+ for num in range (1 , n + 1 ):
49
+ dp [num ] = dp [num // 2 ] + (num % 2 )
50
+ return dp
51
+
15
52
You canโt perform that action at this time.
0 commit comments