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+ # ๋ชจ๋ ๊ฒฝ์ฐ์ ์๋ฅผ ํ์ํ๊ธฐ ์ํด ๋ฐฑํธ๋ํน ์ฌ์ฉ
3+ # - ํ์ฌ ์กฐํฉ์ ํฉ์ด target๋ณด๋ค ํฌ๋ฉด ์ข
๋ฃ
4+ # - ํ์ฌ ์กฐํฉ์ ํฉ์ด target๊ณผ ๊ฐ์ผ๋ฉด ๊ฒฐ๊ณผ์ ์ถ๊ฐ
5+
6+ class Solution :
7+ def combinationSum (self , candidates : List [int ], target : int ) -> List [List [int ]]:
8+ def backtrack (start , combination ):
9+ if sum (combination ) > target :
10+ return
11+ if sum (combination ) == target :
12+ result .append (combination [:])
13+ return
14+ for i in range (start , len (candidates )):
15+ combination .append (candidates [i ])
16+ backtrack (i , combination )
17+ combination .pop ()
18+
19+ result = []
20+ backtrack (0 , [])
21+ return result
Original file line number Diff line number Diff line change 1+ # ๋ฌธ์ ํ์ด
2+ # 1. ์
๋ ฅ๊ฐ n์ binary ํํ๋ก ๋ณํ
3+ # - ์
๋ ฅ๊ฐ n์ด 1๋ณด๋ค ํฌ๋ฉด 2๋ก ๋๋ ๋ชซ๊ณผ ๋๋จธ์ง ๊ณ์ฐ
4+ # - ๋๋จธ์ง๋ฅผ ๋ฆฌ์คํธ์ ์ถ๊ฐํด ๋ฐํ
5+ # 2. ๋ฐํ๋ ๋ฆฌ์คํธ์์ 1์ ๊ฐฏ์ ๋ฐํ
6+
7+ # ์๊ฐ๋ณต์ก๋ O(log n): n์ 2๋ก ๋๋๋ฉด์ ์ฌ๊ท ํจ์ ์คํ
8+ # ๊ณต๊ฐ๋ณต์ก๋ O(log n): ๋นํธ๋ฅผ ์ ์ฅํ๋ ๋ฆฌ์คํธ์ ๊ธธ์ด
9+
10+ class Solution :
11+ def devide_by_2 (self , n , temp ):
12+ if n > 1 :
13+ temp .append (n % 2 )
14+ return self .devide_by_2 (n // 2 , temp )
15+ temp .append (1 )
16+ return n , temp
17+
18+ def hammingWeight (self , n : int ) -> int :
19+ result = []
20+ n , result = self .devide_by_2 (n , result )
21+ return result .count (1 )
Original file line number Diff line number Diff line change 1+ # ๋ฌธ์ ํ์ด
2+ # 1. ๋ฌธ์์ด์ ์๋ฌธ์๋ก ๋ณํ ํ ๋ฌธ์์ ์ซ์๊ฐ ์๋ ๊ฐ์ ์ ๊ฑฐ
3+ # 2. ๋ฌธ์์ด์ ๋ค์ง์ ํ ์๋ ๋ฌธ์์ด๊ณผ ๋น๊ต
4+
5+ # ์๊ฐ๋ณต์ก๋ O(n): ๋ฌธ์์ด ์ฒ๋ฆฌ(re.sub, reversed ๋ฑ) ์ฌ์ฉ
6+ # ๊ณต๊ฐ๋ณต์ก๋ O(n): ์๋ ๋ฌธ์์ด ๋งํผ์ ๊ณต๊ฐ ์ฌ์ฉ
7+ import re
8+
9+ class Solution :
10+ def isPalindrome (self , s : str ) -> bool :
11+ cleaned_s = re .sub (r'[^a-zA-Z0-9]' , '' , s .lower ())
12+ reversed_s = '' .join (reversed (cleaned_s ))
13+ if cleaned_s == reversed_s :
14+ return True
15+ else :
16+ return False
You canโt perform that action at this time.
0 commit comments