Skip to content

Commit f6a9a4e

Browse files
authored
Merge pull request #2118 from Donghae0230/main
[Donghae0230] WEEK 03 solutions
2 parents eb35585 + e99bf84 commit f6a9a4e

File tree

3 files changed

+58
-0
lines changed

3 files changed

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

0 commit comments

Comments
ย (0)