File tree Expand file tree Collapse file tree 3 files changed +58
-0
lines changed
Expand file tree Collapse file tree 3 files changed +58
-0
lines changed Original file line number Diff line number Diff line change 1+ '''
2+ Approach
3+ - target๊ฐ์์ candidate ๊ฐ์ ๋นผ๊ณ ๊ทธ ๋์ ํฉ์ ์ฌ์ฉํด์ผํ๋ค๋ ํ๋ฆ์ ํ์
ํ์ง์ง๋ง ๊ตฌํ์ด ์ด๋ ค์ ์ต๋๋ค.
4+ -๊ทธ๋์ ์๊ณ ๋ฌ๋ ๋ฅผ ์ฐธ๊ณ ํด์ ์ต๋ํ ์ดํดํ๊ณ ํผ์ ์์ฑํด๋ณด๋ ค๊ณ ํ์ต๋๋ค....
5+ '''
6+ class Solution :
7+ def combinationSum (self , candidates : List [int ], target : int ) -> List [List [int ]]:
8+ dp = [[] for _ in range (target + 1 )]
9+ dp [0 ] = [[]]
10+
11+ for candidate in candidates :
12+ for num in range (candidate , target + 1 ):
13+ for combination in dp [num - candidate ]:
14+ dp [num ].append (combination + [candidate ])
15+ return dp [target ]
Original file line number Diff line number Diff line change 1+ '''
2+ Approach
3+ - ์ญ์ง๋ฒ์ ์ด์ง๋ฒ์ผ๋ก ๋ณํํ๋ ๋ฐฉ์๊ณผ ๋์ ํฉ์ ์ฌ์ฉํจ
4+
5+ Time Complexity: O(log n)
6+ - while ๋ฌธ์์ ์ซ์(num)์ ๊ณ์ํด์ 2๋ก ๋๋๋๋ฐ ์์๋๋ ์๊ฐ
7+
8+ Space Complexity: O(1)
9+ - ์์ bits์ nums๋ฅผ ์ ์ฅํ๋ ๊ณต๊ฐ
10+ '''
11+ class Solution :
12+ def hammingWeight (self , n : int ) -> int :
13+ bits = 0
14+ # ์ฃผ์ด์ง ์ซ์๋ฅผ 2๋ก ๋๋ ๋๋จธ์ง ๊ฐ์ด 1์ธ ๊ฒฝ์ฐ bits์ ๋์ ํจ
15+ num = n
16+ while num != 0 :
17+ if num % 2 == 1 :
18+ bits += 1
19+ num //= 2
20+ return bits
Original file line number Diff line number Diff line change 1+ '''
2+ Approach
3+ - ํ์ด์ฌ์์ ์ ๊ณต๋๋ ๋ฉ์๋๋ฅผ ์ฌ์ฉํด์ ๋ฌธ์์ด ํํฐ๋ง ์ฒ๋ฆฌํ ์ผ์ชฝ๊ณผ ์ค๋ฅธ์ชฝ ๊ฐ์ ๋น๊ต
4+
5+ Time Complexity: O(N)
6+ - ๋ฌธ์์ด s์ ๊ธธ์ด๋ฅผ N์ด๋ผ๊ณ ํ ๋, ๋ฌธ์์ด์ ์ํํ๋ฉฐ ํํฐ๋งํ๋ ๋ฐ O(N) ์๊ฐ์ด ๊ฑธ๋ฆผ
7+ - ์ดํ ํํฐ๋ง๋ ๋ฌธ์์ด์ ๋ค์ง์ด right ๊ฐ์ ๋ง๋๋ ๋ฐ๋ O(N) ์๊ฐ์ด ๊ฑธ๋ฆผ
8+ - left์ right ๊ฐ์ ๋น๊ตํ๋ ๋ฐ๋ O(N) ์๊ฐ์ด ๊ฑธ๋ฆผ
9+
10+ Space Complexity: O(N)
11+ - ํํฐ๋ง๋ ๋ฌธ์์ด์ ์ ์ฅํ๋ ๋ฐ O(N) ๊ณต๊ฐ์ด ํ์ํจ
12+ - left์ right ๊ฐ์ ์ ์ฅํ๋ ๋ฐ๋ ๊ฐ๊ฐ O(N) ๊ณต๊ฐ์ด ํ์ํจ
13+ '''
14+ class Solution :
15+ def isPalindrome (self , s : str ) -> bool :
16+ # ๋ฌธ์์ด์ด ์๋ ๊ฒฝ์ฐ๋ง ์๋ฌธ์ ๋ณํ ํํฐ๋ง
17+ char_str = [char .lower () for char in s if char .isalnum ()]
18+ # ์ผ์ชฝ ๊ฐ
19+ left = '' .join (char_str )
20+ # ์ค๋ฅธ์ชฝ ๊ฐ
21+ right = '' .join (char_str [::- 1 ])
22+
23+ return left == right
You canโt perform that action at this time.
0 commit comments