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 11"""
22[๋ฌธ์ ํ์ด]
33# Inputs
4-
4+ n: int
55# Outputs
66
7+ ans: arr
8+ ๊ธธ์ด: n + 1
9+
10+ ans๋ด ๊ฐ ์์๋ค : ans[i]: i์ ์ด์ง๋ฒ์์ 1์ ๊ฐ์
11+
712# Constraints
13+ 0 <= n <= 10^5
814
915# Ideas
16+ 2์ค for๋ฌธ ์ด๋ฉด ์๋จ
1017
1118[ํ๊ณ ]
12-
19+ dp๋ฅผ ํ์ฉํ ํ์ด๋ ๊ฐ์ด ์์๋์
1320"""
1421
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+
1552
You canโt perform that action at this time.
0 commit comments