Skip to content

Commit 7dbbb81

Browse files
committed
first commit
1 parent ee7032e commit 7dbbb81

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

189 files changed

+4329
-0
lines changed

Easy/1.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class Solution(object):
2+
def twoSum(self, nums, target):
3+
idx = {}
4+
for i in range(len(nums)):
5+
comp = target - nums[i]
6+
if comp in idx:
7+
return [i, idx[comp]]
8+
idx[nums[i]] = i
9+
return []

Easy/100.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Definition for a binary tree node.
2+
from typing import Optional
3+
4+
5+
class TreeNode:
6+
def __init__(self, val=0, left=None, right=None):
7+
self.val = val
8+
self.left = left
9+
self.right = right
10+
11+
12+
class Solution:
13+
def isSameTree(self, p: Optional[TreeNode], q: Optional[TreeNode]) -> bool:
14+
if not p and not q:
15+
return True
16+
if (p == None) != (q == None):
17+
return False
18+
if p.val != q.val:
19+
return False
20+
return (
21+
True
22+
and self.isSameTree(p.right, q.right)
23+
and self.isSameTree(p.left, q.left)
24+
)

Easy/104.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Definition for a binary tree node.
2+
from typing import Optional
3+
4+
5+
class TreeNode:
6+
def __init__(self, val=0, left=None, right=None):
7+
self.val = val
8+
self.left = left
9+
self.right = right
10+
11+
12+
class Solution:
13+
def maxDepth(self, root: Optional[TreeNode]) -> int:
14+
if root is None:
15+
return 0
16+
17+
max_depth = max(self.maxDepth(root.left), self.maxDepth(root.right))
18+
19+
return max_depth + 1

Easy/1046.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
from typing import List
2+
import heapq
3+
4+
5+
class Solution:
6+
def lastStoneWeight(self, stones: List[int]) -> int:
7+
stones = [-i for i in stones]
8+
heapq.heapify(stones)
9+
10+
while len(stones) > 1:
11+
y = -heapq.heappop(stones)
12+
x = -heapq.heappop(stones)
13+
newStone = y - x
14+
if newStone > 0:
15+
heapq.heappush(stones, -newStone)
16+
17+
return -stones[0] if len(stones) > 0 else 0

Easy/110.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Definition for a binary tree node.
2+
from typing import Optional
3+
4+
5+
class TreeNode:
6+
def __init__(self, val=0, left=None, right=None):
7+
self.val = val
8+
self.left = left
9+
self.right = right
10+
11+
12+
class Solution:
13+
res = True
14+
15+
def isBalanced(self, root: Optional[TreeNode]) -> bool:
16+
def dfs(root):
17+
if not root:
18+
return 0
19+
20+
left = dfs(root.left)
21+
right = dfs(root.right)
22+
23+
if left - right > 1 or right - left > 1:
24+
self.res = self.res and False
25+
26+
return max(left, right) + 1
27+
28+
dfs(root)
29+
return self.res

Easy/121.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
from typing import List
2+
3+
4+
class Solution:
5+
def maxProfit(self, prices: List[int]) -> int:
6+
curr_prof, max_prof = prices[0], 0
7+
8+
for p in prices[1:]:
9+
if curr_prof > p:
10+
curr_prof = p
11+
max_prof = max(max_prof, p - curr_prof)
12+
13+
return max_prof

Easy/125.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Solution:
2+
def isPalindrome(self, s: str) -> bool:
3+
l, r = 0, len(s) - 1
4+
5+
while l < r:
6+
while l < r and not s[l].isalnum():
7+
l += 1
8+
while l < r and not s[r].isalnum():
9+
r -= 1
10+
if s[l].lower() != s[r].lower():
11+
return False
12+
r -= 1
13+
l += 1
14+
return True

Easy/13.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Solution:
2+
def romanToInt(self, s: str) -> int:
3+
mapp = {"I": 1, "V": 5, "X": 10, "L": 50, "C": 100, "D": 500, "M": 1000}
4+
res = mapp[s[-1]]
5+
for i in range(len(s) - 2, -1, -1):
6+
if mapp[s[i]] < mapp[s[i + 1]]:
7+
res -= mapp[s[i]]
8+
else:
9+
res += mapp[s[i]]
10+
11+
return res

Easy/136.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
from typing import List
2+
3+
4+
class Solution:
5+
def singleNumber(self, nums: List[int]) -> int:
6+
res = 0
7+
for num in nums:
8+
res = res ^ num
9+
return res

Easy/14.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
from typing import List
2+
3+
4+
class Solution:
5+
def longestCommonPrefix(self, strs: List[str]) -> str:
6+
if not strs:
7+
return ""
8+
9+
res = ""
10+
11+
for i in range(len(strs[0])):
12+
char = strs[0][i]
13+
for s in strs[1:]:
14+
# Stop if i exceeds length or mismatch found
15+
if i >= len(s) or s[i] != char:
16+
return res
17+
res += char
18+
return res

0 commit comments

Comments
 (0)