File tree Expand file tree Collapse file tree 3 files changed +47
-0
lines changed
Expand file tree Collapse file tree 3 files changed +47
-0
lines changed Original file line number Diff line number Diff line change 1+ class Solution :
2+ def combinationSum (self , candidates : List [int ], target : int ) -> List [List [int ]]:
3+ def backtrack (start , combination ):
4+ if sum (combination ) > target :
5+ return
6+ if sum (combination ) == target :
7+ result .append (combination [:])
8+ return
9+ for i in range (start , len (candidates )):
10+ combination .append (candidates [i ])
11+ backtrack (i , combination )
12+ combination .pop ()
13+
14+ result = []
15+ backtrack (0 , [])
16+ return result
Original file line number Diff line number Diff line change 1+ # 시간 복잡도 O(n): 2로 반복해서 나눠야함
2+ # 공간 복잡도 O(n): result 생성
3+
4+ class Solution :
5+ def devide_by_2 (self , n , temp ):
6+ global val
7+ if n > 1 :
8+ val = n // 2
9+ remain = n % 2
10+ temp .append (remain )
11+ return self .devide_by_2 (val , temp )
12+ temp .append (val )
13+ return val , temp
14+
15+ def hammingWeight (self , n : int ) -> int :
16+ result = []
17+ n , result = self .devide_by_2 (n , result )
18+ return result .count (1 )
19+
Original file line number Diff line number Diff line change 1+ # 시간복잡도 O(n): reversed 함수 사용
2+ # 공간복잡도 O(n): cleaned_s, reversed_s 사용
3+ import re
4+
5+ class Solution :
6+ def isPalindrome (self , s : str ) -> bool :
7+ cleaned_s = re .sub (r'[^a-zA-Z0-9]' , '' , s .lower ())
8+ reversed_s = '' .join (reversed (cleaned_s ))
9+ if cleaned_s == reversed_s :
10+ return True
11+ else :
12+ return False
You can’t perform that action at this time.
0 commit comments