-
Notifications
You must be signed in to change notification settings - Fork 126
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #762 from HodaeSsi/main
[HodaeSsi] Week 02
- Loading branch information
Showing
5 changed files
with
92 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
class Solution: | ||
def threeSum(self, nums: List[int]) -> List[List[int]]: | ||
answerSet = set() | ||
nums.sort() | ||
|
||
for i in range(len(nums) - 2): | ||
leftIdx = i + 1 | ||
rightIdx = len(nums) - 1 | ||
while leftIdx < rightIdx: | ||
sum = nums[i] + nums[leftIdx] + nums[rightIdx] | ||
if sum < 0: | ||
leftIdx += 1 | ||
elif sum > 0: | ||
rightIdx -= 1 | ||
else: | ||
answerSet.add((nums[i], nums[leftIdx], nums[rightIdx])) | ||
leftIdx = leftIdx + 1 | ||
rightIdx = rightIdx - 1 | ||
|
||
return list(answerSet) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
class Solution: | ||
def climbStairs(self, n: int) -> int: | ||
dp = [] | ||
dp.append(0) | ||
dp.append(1) | ||
dp.append(2) | ||
|
||
for i in range(3, n + 1): | ||
dp.append(dp[i - 1] + dp[i - 2]) | ||
|
||
return dp[n] | ||
|
12 changes: 12 additions & 0 deletions
12
construct-binary-tree-from-preorder-and-inorder-traversal/HodaeSsi.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
class Solution: | ||
def buildTree(self, preorder: List[int], inorder: List[int]) -> Optional[TreeNode]: | ||
if inorder == []: | ||
return None | ||
|
||
mid = preorder.pop(0) | ||
midIdx = inorder.index(mid) | ||
left = self.buildTree(preorder, inorder[:midIdx]) | ||
right = self.buildTree(preorder, inorder[midIdx + 1:]) | ||
|
||
return TreeNode(mid, left, right) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
class Solution: | ||
def numDecodings(self, s: str) -> int: | ||
dp = [] | ||
if (s[0] == '0'): | ||
return 0 | ||
dp.append(1) | ||
|
||
for idx, _ in enumerate(s): | ||
if idx == 0: | ||
continue | ||
if s[idx] == '0': | ||
if s[idx-1] == '1' or s[idx-1] == '2': | ||
if idx == 1: | ||
dp.append(1) | ||
else: | ||
dp.append(dp[idx-2]) | ||
else: | ||
return 0 | ||
elif s[idx-1] == '1' or (s[idx-1] == '2' and (s[idx] >= '1' and s[idx] <= '6')): | ||
if idx == 1: | ||
dp.append(2) | ||
else: | ||
dp.append(dp[idx-1] + dp[idx-2]) | ||
else: | ||
dp.append(dp[idx-1]) | ||
|
||
return dp[-1] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
class Solution: | ||
def isAnagram(self, s: str, t: str) -> bool: | ||
sLetterDict = {} | ||
tLetterDict = {} | ||
|
||
for letter in s: | ||
sLetterDict[letter] = sLetterDict.get(letter, 0) + 1 | ||
for letter in t: | ||
tLetterDict[letter] = tLetterDict.get(letter, 0) + 1 | ||
|
||
if len(sLetterDict) != len(tLetterDict): | ||
return False | ||
|
||
for sKey in sLetterDict.keys(): | ||
if sKey not in tLetterDict or sLetterDict[sKey] != tLetterDict[sKey]: | ||
return False | ||
|
||
return True | ||
|