Skip to content

Commit 23a5596

Browse files
authored
Merge pull request #1263 from Sung-Heon/main
[Sung-Heon]WEEK 02 solution
2 parents 2ff3d90 + ca3b663 commit 23a5596

File tree

5 files changed

+89
-0
lines changed

5 files changed

+89
-0
lines changed

3sum/Sung-Heon.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
class Solution:
2+
def threeSum(self, nums: List[int]) -> List[List[int]]:
3+
nums.sort()
4+
result = []
5+
n = len(nums)
6+
7+
for i in range(n - 2):
8+
if i > 0 and nums[i] == nums[i - 1]:
9+
continue
10+
11+
left = i + 1
12+
right = n - 1
13+
14+
while left < right:
15+
current_sum = nums[i] + nums[left] + nums[right]
16+
17+
if current_sum == 0:
18+
result.append([nums[i], nums[left], nums[right]])
19+
20+
while left < right and nums[left] == nums[left + 1]:
21+
left += 1
22+
while left < right and nums[right] == nums[right - 1]:
23+
right -= 1
24+
25+
left += 1
26+
right -= 1
27+
28+
elif current_sum < 0:
29+
left += 1
30+
else:
31+
right -= 1
32+
33+
return result

climbing-stairs/Sung-Heon.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution:
2+
def climbStairs(self, n: int) -> int:
3+
temp = {}
4+
if n == 1:
5+
return 1
6+
if n == 2:
7+
return 2
8+
temp[1] = 1
9+
temp[2] = 2
10+
a = 2
11+
while a != n:
12+
a += 1
13+
temp[a] = temp[a - 1] + temp[a - 2]
14+
return temp[a]
15+
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Solution:
2+
def productExceptSelf(self, nums: List[int]) -> List[int]:
3+
n = len(nums)
4+
answer = [1] * n
5+
left_product = 1
6+
for i in range(n):
7+
answer[i] = left_product
8+
left_product *= nums[i]
9+
right_product = 1
10+
for i in range(n - 1, -1, -1):
11+
answer[i] *= right_product
12+
right_product *= nums[i]
13+
14+
return answer

valid-anagram/Sung-Heon.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution:
2+
def isAnagram(self, s: str, t: str) -> bool:
3+
s = list(s)
4+
t = list(t)
5+
try:
6+
for i in s:
7+
if len(t) == 0:
8+
return False
9+
t.remove(i)
10+
except:
11+
return False
12+
if len(t) == 0:
13+
return True
14+
else:
15+
return False
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Solution:
2+
def isValidBST(self, root):
3+
def validate(node, min_val, max_val):
4+
if not node:
5+
return True
6+
7+
if node.val <= min_val or node.val >= max_val:
8+
return False
9+
10+
return validate(node.left, min_val, node.val) and validate(node.right, node.val, max_val)
11+
12+
return validate(root, float('-inf'), float('inf'))

0 commit comments

Comments
 (0)