Skip to content

Commit 75cdcb3

Browse files
committed
Leetcode submissions
1 parent 26c76f3 commit 75cdcb3

38 files changed

+710
-0
lines changed

a-number-after-a-double-reversal.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class Solution:
2+
def isSameAfterReversals(self, num: int) -> bool:
3+
i = 0
4+
if num == 0:
5+
return True
6+
while num % 10 == 0:
7+
num = num // 10
8+
i += 1
9+
return i == 0
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import heapq
2+
3+
# Definition for a binary tree node.
4+
# class TreeNode:
5+
# def __init__(self, val=0, left=None, right=None):
6+
# self.val = val
7+
# self.left = left
8+
# self.right = right
9+
class Solution:
10+
def getAllElements(self, root1: TreeNode, root2: TreeNode) -> List[int]:
11+
heap = []
12+
paths = [root1, root2]
13+
i = 0
14+
while i < len(paths):
15+
curr = paths[i]
16+
if curr:
17+
heapq.heappush(heap, curr.val)
18+
if curr.left:
19+
paths.append(curr.left)
20+
if curr.right:
21+
paths.append(curr.right)
22+
i += 1
23+
return heapq.nsmallest(len(heap), heap)

basic-calculator-ii.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
class Solution:
2+
def eval(self, a, op, b):
3+
if op == '+':
4+
return a + b
5+
elif op == '-':
6+
return a - b
7+
elif op == '/':
8+
return int(a / b)
9+
elif op == '*':
10+
return a * b
11+
12+
def calculate(self, s: str) -> int:
13+
s += " "
14+
precedence = {
15+
'*': 1,
16+
'/': 1,
17+
'+': 0,
18+
'-': 0
19+
}
20+
numstack = []
21+
opstack = []
22+
chunk = ""
23+
for c in s:
24+
if c == " " or c in precedence:
25+
if len(chunk) > 0:
26+
numstack.append(int(chunk))
27+
chunk = ""
28+
if c in precedence:
29+
while len(opstack) > 0 and precedence[opstack[-1]] >= precedence[c]:
30+
currop = opstack.pop()
31+
b = numstack.pop()
32+
a = numstack.pop()
33+
res = self.eval(a, currop, b)
34+
numstack.append(res)
35+
opstack.append(c)
36+
else:
37+
chunk += c
38+
while len(opstack) > 0:
39+
currop = opstack.pop()
40+
b = numstack.pop()
41+
a = numstack.pop()
42+
res = self.eval(a, currop, b)
43+
numstack.append(res)
44+
return numstack[0]

basic-calculator.py

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
class Solution:
2+
def eval(self, a, op, b):
3+
if op == '+':
4+
return a + b
5+
elif op == '-':
6+
return a - b
7+
elif op == '/':
8+
return int(a / b)
9+
elif op == '*':
10+
return a * b
11+
12+
def calculate(self, s: str) -> int:
13+
s += " "
14+
precedence = {
15+
'*': 1,
16+
'/': 1,
17+
'+': 0,
18+
'-': 0
19+
}
20+
brackets = {
21+
'(': 1,
22+
')': -1
23+
}
24+
numstack = []
25+
opstack = []
26+
chunk = ""
27+
latestOpenBracket = True
28+
for c in s:
29+
if c == " " or c in precedence or c in brackets:
30+
if len(chunk) > 0:
31+
numstack.append(int(chunk))
32+
chunk = ""
33+
if c in brackets:
34+
if brackets[c] == 1:
35+
opstack.append('(')
36+
latestOpenBracket = True
37+
elif brackets[c] == -1:
38+
latestOpenBracket = False
39+
while opstack[-1] != '(':
40+
currop = opstack.pop()
41+
b = numstack.pop()
42+
a = numstack.pop()
43+
res = self.eval(a, currop, b)
44+
numstack.append(res)
45+
opstack.pop()
46+
elif c in precedence:
47+
if latestOpenBracket:
48+
numstack.append(0)
49+
while len(opstack) > 0 and precedence.get(opstack[-1], -1) >= precedence[c]:
50+
currop = opstack.pop()
51+
b = numstack.pop()
52+
a = numstack.pop()
53+
res = self.eval(a, currop, b)
54+
numstack.append(res)
55+
opstack.append(c)
56+
latestOpenBracket = False
57+
else:
58+
chunk += c
59+
latestOpenBracket = False
60+
while len(opstack) > 0:
61+
currop = opstack.pop()
62+
b = numstack.pop()
63+
a = numstack.pop()
64+
res = self.eval(a, currop, b)
65+
numstack.append(res)
66+
return numstack[0]

capitalize-the-title.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
class Solution:
2+
def capitalizeTitle(self, title: str) -> str:
3+
words = title.split()
4+
words = [word.lower() for word in words]
5+
words = [(word[0].upper() + word[1:]) if len(word) > 2 else word for word in words]
6+
return " ".join(words)

car-pooling.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import bisect
2+
3+
class Solution:
4+
def carPooling(self, trips: List[List[int]], capacity: int) -> bool:
5+
n = len(trips)
6+
points = []
7+
for i in range(n):
8+
num, source, dest = trips[i]
9+
bisect.insort(points, (source, num))
10+
bisect.insort(points, (dest, -num))
11+
k = len(points)
12+
curr = 0
13+
for i in range(k):
14+
curr += points[i][1]
15+
if curr > capacity:
16+
return False
17+
return True

check-if-it-is-a-good-array.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Solution:
2+
def gcd(self, a, b):
3+
while b:
4+
a, b = b, a % b
5+
return a
6+
7+
def isGoodArray(self, nums: List[int]) -> bool:
8+
n = len(nums)
9+
curr = nums[0]
10+
for i in range(1, n):
11+
curr = self.gcd(curr, nums[i])
12+
return curr == 1

combine-two-tables.sql

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
SELECT firstName, lastName, city, state from Person LEFT JOIN Address ON Person.personId = Address.personId;

count-square-sum-triples.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Solution:
2+
def countTriples(self, n: int) -> int:
3+
exists = set()
4+
for c in range(1, n + 1):
5+
exists.add(c * c)
6+
ctr = 0
7+
for a in range(1, n + 1):
8+
for b in range(1, n + 1):
9+
if (a * a + b * b) in exists:
10+
ctr += 1
11+
return ctr

detect-capital.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Solution:
2+
def detectCapitalUse(self, word: str) -> bool:
3+
n = len(word)
4+
caps = 0
5+
for i in range(n):
6+
if word[i].isupper():
7+
caps += 1
8+
if caps == 0 or caps == n:
9+
return True
10+
if caps == 1 and word[0].isupper():
11+
return True
12+
return False

0 commit comments

Comments
 (0)