Skip to content

Commit 28bf5d3

Browse files
authored
Merge pull request #2461 from ohkingtaek/main
[ohkingtaek] Week 03 solutions
2 parents 313fa69 + 74c8ccc commit 28bf5d3

File tree

3 files changed

+52
-0
lines changed

3 files changed

+52
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
class Solution:
2+
def combinationSum(self, candidates: List[int], target: int) -> List[List[int]]:
3+
"""
4+
- ์‹œ๊ฐ„๋ณต์žก๋„: O(n^2)
5+
- ๊ณต๊ฐ„๋ณต์žก๋„: O(n)
6+
DFS ์‚ฌ์šฉ
7+
1. ๋ชจ๋“  ์กฐํ•ฉ์„ ์ฐพ๊ธฐ
8+
2. ์กฐํ•ฉ์„ ์ฐพ์„ ๋•Œ, ์ค‘๋ณต๋œ ์กฐํ•ฉ์„ ๋ฐฉ์ง€ํ•˜๊ธฐ
9+
"""
10+
result = []
11+
12+
def dfs(start, path, total):
13+
if total == target:
14+
result.append(path[:])
15+
return
16+
if total > target:
17+
return
18+
19+
for i in range(start, len(candidates)):
20+
path.append(candidates[i])
21+
dfs(i, path, total + candidates[i])
22+
path.pop()
23+
24+
dfs(0, [], 0)
25+
return result
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
from collections import Counter
2+
3+
class Solution:
4+
def hammingWeight(self, n: int) -> int:
5+
"""
6+
- ์‹œ๊ฐ„๋ณต์žก๋„: O(n)
7+
- ๊ณต๊ฐ„๋ณต์žก๋„: O(n)
8+
๋ฌธ์ž์—ด์„ ์ด์ง„์ˆ˜๋กœ ๋ณ€ํ™˜ํ•˜๊ณ , 1์˜ ๊ฐœ์ˆ˜๋ฅผ ์„ธ๊ธฐ
9+
"""
10+
return Counter(bin(n)[2:])["1"]
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution:
2+
def isPalindrome(self, s: str) -> bool:
3+
"""
4+
- ์‹œ๊ฐ„๋ณต์žก๋„: O(n)
5+
- ๊ณต๊ฐ„๋ณต์žก๋„: O(n)
6+
1. ๋ฌธ์ž์—ด์„ ์†Œ๋ฌธ์ž๋กœ ๋ณ€ํ™˜ํ•˜๊ณ , ์•ŒํŒŒ๋ฒณ๊ณผ ์ˆซ์ž๋งŒ ๋‚จ๊ธฐ๊ธฐ
7+
2. ๋‚จ์€ ๋ฌธ์ž์—ด์„ ์ ˆ๋ฐ˜์œผ๋กœ ๋‚˜๋ˆ„๊ณ , ์•ž๊ณผ ๋’ค๋ฅผ ๋น„๊ตํ•˜์—ฌ ํšŒ๋ฌธ์ธ์ง€ ํ™•์ธ
8+
3. ํšŒ๋ฌธ์ด๋ฉด True, ์•„๋‹ˆ๋ฉด False ๋ฐ˜ํ™˜
9+
"""
10+
a = ""
11+
for i in s.lower():
12+
if i.isalnum():
13+
a += i
14+
for i, j in zip(a[:len(a) // 2], a[len(a):(len(a) // 2 - 1):-1]):
15+
if i != j:
16+
return False
17+
return True

0 commit comments

Comments
ย (0)