Skip to content

Commit 43f5f8f

Browse files
committed
3주차 solution
1 parent a8c940a commit 43f5f8f

File tree

3 files changed

+47
-0
lines changed

3 files changed

+47
-0
lines changed

combination-sum/Donghae0230.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
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

number-of-1-bits/Donghae0230.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
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+

valid-palindrome/Donghae0230.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
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

0 commit comments

Comments
 (0)