Skip to content

Commit 8f45cfd

Browse files
authored
Merge pull request #762 from HodaeSsi/main
[HodaeSsi] Week 02
2 parents e5cf9af + 105a6b8 commit 8f45cfd

File tree

5 files changed

+92
-0
lines changed

5 files changed

+92
-0
lines changed

3sum/HodaeSsi.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Solution:
2+
def threeSum(self, nums: List[int]) -> List[List[int]]:
3+
answerSet = set()
4+
nums.sort()
5+
6+
for i in range(len(nums) - 2):
7+
leftIdx = i + 1
8+
rightIdx = len(nums) - 1
9+
while leftIdx < rightIdx:
10+
sum = nums[i] + nums[leftIdx] + nums[rightIdx]
11+
if sum < 0:
12+
leftIdx += 1
13+
elif sum > 0:
14+
rightIdx -= 1
15+
else:
16+
answerSet.add((nums[i], nums[leftIdx], nums[rightIdx]))
17+
leftIdx = leftIdx + 1
18+
rightIdx = rightIdx - 1
19+
20+
return list(answerSet)
21+

climbing-stairs/HodaeSsi.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Solution:
2+
def climbStairs(self, n: int) -> int:
3+
dp = []
4+
dp.append(0)
5+
dp.append(1)
6+
dp.append(2)
7+
8+
for i in range(3, n + 1):
9+
dp.append(dp[i - 1] + dp[i - 2])
10+
11+
return dp[n]
12+
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Solution:
2+
def buildTree(self, preorder: List[int], inorder: List[int]) -> Optional[TreeNode]:
3+
if inorder == []:
4+
return None
5+
6+
mid = preorder.pop(0)
7+
midIdx = inorder.index(mid)
8+
left = self.buildTree(preorder, inorder[:midIdx])
9+
right = self.buildTree(preorder, inorder[midIdx + 1:])
10+
11+
return TreeNode(mid, left, right)
12+

decode-ways/HodaeSsi.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
class Solution:
2+
def numDecodings(self, s: str) -> int:
3+
dp = []
4+
if (s[0] == '0'):
5+
return 0
6+
dp.append(1)
7+
8+
for idx, _ in enumerate(s):
9+
if idx == 0:
10+
continue
11+
if s[idx] == '0':
12+
if s[idx-1] == '1' or s[idx-1] == '2':
13+
if idx == 1:
14+
dp.append(1)
15+
else:
16+
dp.append(dp[idx-2])
17+
else:
18+
return 0
19+
elif s[idx-1] == '1' or (s[idx-1] == '2' and (s[idx] >= '1' and s[idx] <= '6')):
20+
if idx == 1:
21+
dp.append(2)
22+
else:
23+
dp.append(dp[idx-1] + dp[idx-2])
24+
else:
25+
dp.append(dp[idx-1])
26+
27+
return dp[-1]
28+

valid-anagram/HodaeSsi.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution:
2+
def isAnagram(self, s: str, t: str) -> bool:
3+
sLetterDict = {}
4+
tLetterDict = {}
5+
6+
for letter in s:
7+
sLetterDict[letter] = sLetterDict.get(letter, 0) + 1
8+
for letter in t:
9+
tLetterDict[letter] = tLetterDict.get(letter, 0) + 1
10+
11+
if len(sLetterDict) != len(tLetterDict):
12+
return False
13+
14+
for sKey in sLetterDict.keys():
15+
if sKey not in tLetterDict or sLetterDict[sKey] != tLetterDict[sKey]:
16+
return False
17+
18+
return True
19+

0 commit comments

Comments
 (0)