Skip to content

Commit 8fc7bd0

Browse files
authored
Merge pull request #2042 from 8804who/main
[8804who] WEEK 02 Solutions
2 parents 96681ef + cec4667 commit 8fc7bd0

File tree

5 files changed

+73
-0
lines changed

5 files changed

+73
-0
lines changed

3sum/8804who.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution:
2+
def threeSum(self, nums: List[int]) -> List[List[int]]:
3+
temp = {}
4+
answer = {}
5+
nums.sort()
6+
for i in range(len(nums)):
7+
for j in range(i+1, len(nums)):
8+
if -(nums[i]+nums[j]) in temp:
9+
ans = str(nums[i])+','+str(nums[j])
10+
answer[ans] = 1
11+
temp[nums[i]] = 1
12+
temp_ans = []
13+
for ans in answer.keys():
14+
s1, s2 = ans.split(',')
15+
temp_ans.append([int(s1), int(s2), -(int(s1)+int(s2))])
16+
return temp_ans
17+

climbing-stairs/8804who.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class Solution:
2+
def climbStairs(self, n: int) -> int:
3+
dp = [1] * (n+1)
4+
5+
for i in range(2, n+1):
6+
dp[i] = dp[i-1]+dp[i-2]
7+
8+
return dp[n]
9+
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
class Solution:
2+
def productExceptSelf(self, nums: List[int]) -> List[int]:
3+
answer = []
4+
5+
product1 = [0] * len(nums)
6+
product2 = [0] * len(nums)
7+
8+
product1[0] = nums[0]
9+
10+
for i in range(1, len(nums)):
11+
product1[i] = product1[i-1]*nums[i]
12+
13+
product2[-1] = nums[-1]
14+
15+
for i in range(len(nums)-2, -1, -1):
16+
product2[i] = product2[i+1] * nums[i]
17+
18+
for i in range(len(nums)):
19+
if i == 0:
20+
answer.append(product2[1])
21+
elif i == len(nums)-1:
22+
answer.append(product1[-2])
23+
else:
24+
answer.append(product1[i-1]*product2[i+1])
25+
return answer
26+

valid-anagram/8804who.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
from collections import Counter
2+
class Solution:
3+
def isAnagram(self, s: str, t: str) -> bool:
4+
return Counter(s) == Counter(t)
5+
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Definition for a binary tree node.
2+
# class TreeNode:
3+
# def __init__(self, val=0, left=None, right=None):
4+
# self.val = val
5+
# self.left = left
6+
# self.right = right
7+
class Solution:
8+
def isValidBST(self, root: Optional[TreeNode]) -> bool:
9+
def dfs(node, min_num, max_num):
10+
if not node:
11+
return True
12+
if max_num <= node.val or node.val <= min_num:
13+
return False
14+
return dfs(node.left, min_num, node.val) and dfs(node.right, node.val, max_num)
15+
return dfs(root, float("-inf"), float("inf"))
16+

0 commit comments

Comments
 (0)