Skip to content

Commit 228c8b0

Browse files
committed
:squirrel:
1 parent 711e987 commit 228c8b0

18 files changed

+477
-0
lines changed

add-strings.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# 415. Add Strings
2+
# https://leetcode.com/problems/add-strings/
3+
4+
class Solution:
5+
def addStrings(self, num1: str, num2: str) -> str:
6+
n1, n2 = len(num1), len(num2)
7+
i, j = n1-1, n2-1
8+
carry = 0
9+
ans = []
10+
while i >= 0 or j >= 0:
11+
s = carry
12+
if i >= 0:
13+
s += int(num1[i])
14+
i -= 1
15+
if j >= 0:
16+
s += int(num2[j])
17+
j -= 1
18+
19+
if s > 9:
20+
carry = 1
21+
else:
22+
carry = 0
23+
24+
ans.append(str(s%10))
25+
26+
if carry == 1:
27+
ans.append('1')
28+
29+
return ''.join(ans[::-1])

best-time-to-buy-and-sell-stock.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# 121. Best Time to Buy and Sell Stock
2+
# https://leetcode.com/problems/best-time-to-buy-and-sell-stock/
3+
4+
class Solution:
5+
def maxProfit(self, prices: List[int]) -> int:
6+
min_price = float('inf')
7+
max_profit = 0
8+
for x in prices:
9+
min_price = min(x, min_price)
10+
max_profit = max(max_profit, x - min_price)
11+
return max_profit

binary-tree-right-side-view.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# 199. Binary Tree Right Side View
2+
# https://leetcode.com/problems/binary-tree-right-side-view/
3+
4+
# Definition for a binary tree node.
5+
# class TreeNode:
6+
# def __init__(self, x):
7+
# self.val = x
8+
# self.left = None
9+
# self.right = None
10+
11+
class Solution:
12+
def rightSideView(self, root: TreeNode) -> List[int]:
13+
if not root:
14+
return []
15+
16+
levels = []
17+
sublevel = []
18+
q = [root, None]
19+
while q:
20+
val = q.pop(0)
21+
if val == None:
22+
# If sublevel it's empty means that we've reach the end
23+
if sublevel:
24+
q.append(None)
25+
levels.append(sublevel[:])
26+
sublevel.clear()
27+
else:
28+
sublevel.append(val.val)
29+
if val.left:
30+
q.append(val.left)
31+
if val.right:
32+
q.append(val.right)
33+
34+
return [x[-1] for x in levels]

clone-graph.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# 133. Clone Graph
2+
# https://leetcode.com/problems/clone-graph/
3+
4+
"""
5+
# Definition for a Node.
6+
class Node:
7+
def __init__(self, val = 0, neighbors = []):
8+
self.val = val
9+
self.neighbors = neighbors
10+
"""
11+
class Solution:
12+
def cloneGraph(self, node: 'Node') -> 'Node':
13+
if not node:
14+
return None
15+
16+
f = {}
17+
q = [node]
18+
19+
f[node] = Node(node.val, [])
20+
21+
while q:
22+
u = q.pop(0)
23+
for v in u.neighbors:
24+
if v not in f:
25+
f[v] = Node(v.val, [])
26+
q.append(v)
27+
f[u].neighbors.append(f[v])
28+
29+
return f[node]

continuous-subarray-sum.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# 523. Continuous Subarray Sum
2+
# https://leetcode.com/problems/continuous-subarray-sum/
3+
4+
class Solution:
5+
def checkSubarraySum(self, nums: List[int], k: int) -> bool:
6+
n = len(nums)
7+
dp = [0]*n
8+
dp[0] = nums[0]
9+
for i in range(1, n):
10+
dp[i] = dp[i-1] + nums[i]
11+
for i in range(n-1):
12+
for j in range(i+1, n):
13+
sum_ = dp[j] - dp[i] + nums[i]
14+
if sum_ == k or (k != 0 and sum_ % k == 0):
15+
return True
16+
return False

copy-list-with-random-pointer.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# 138. Copy List with Random Pointer
2+
# https://leetcode.com/problems/copy-list-with-random-pointer/
3+
4+
"""
5+
# Definition for a Node.
6+
class Node:
7+
def __init__(self, x: int, next: 'Node' = None, random: 'Node' = None):
8+
self.val = int(x)
9+
self.next = next
10+
self.random = random
11+
"""
12+
class Solution:
13+
def copyRandomList(self, head: 'Node') -> 'Node':
14+
if not head:
15+
return None
16+
17+
p = head
18+
mapping = dict()
19+
20+
while p:
21+
mapping[p] = Node(p.val)
22+
p = p.next
23+
24+
p = head
25+
26+
while p:
27+
aux = mapping[p]
28+
aux.next = mapping.get(p.next, None)
29+
aux.random = mapping.get(p.random, None)
30+
31+
p = p.next
32+
33+
return mapping[head]

decode-ways.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# 91. Decode Ways
2+
# https://leetcode.com/problems/decode-ways/
3+
4+
class Solution:
5+
def numDecodings(self, s: str) -> int:
6+
def aux(s, dp, i, n):
7+
if i == n:
8+
return 1
9+
elif i > n:
10+
return 0
11+
if dp[i] == -1:
12+
c = 0
13+
if s[i] != 0:
14+
c += aux(s, dp, i+1, n)
15+
if i+1 < n and (s[i] == 1 or (s[i] == 2 and s[i+1] < 7)):
16+
c += aux(s, dp, i+2, n)
17+
dp[i] = c
18+
return dp[i]
19+
n = len(s)
20+
dp = [-1]*n
21+
nums = [int(x) for x in s]
22+
return aux(nums, dp, 0, n)
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# 34. Find First and Last Position of Element in Sorted Array
2+
# https://leetcode.com/problems/find-first-and-last-position-of-element-in-sorted-array/
3+
4+
class Solution:
5+
def searchRange(self, nums: List[int], target: int) -> List[int]:
6+
def binary_search_mod(start, end, target, nums, first):
7+
ans = -1
8+
while start < end:
9+
m = start + (end-start)//2
10+
if nums[m] == target:
11+
ans = m
12+
if first:
13+
end = m
14+
else:
15+
start = m+1
16+
elif nums[m] < target:
17+
start = m+1
18+
else:
19+
end = m
20+
return ans
21+
22+
n = len(nums)
23+
fst = binary_search_mod(0, n, target, nums, True)
24+
snd = binary_search_mod(0, n, target, nums, False)
25+
return [fst, snd]

find-the-duplicate-number.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# 287. Find the Duplicate Number
2+
# https://leetcode.com/problems/find-the-duplicate-number/
3+
4+
class Solution:
5+
def findDuplicate(self, nums: List[int]) -> int:
6+
nums.sort()
7+
for i in range(len(nums)-1):
8+
if nums[i] == nums[i+1]:
9+
return nums[i]
10+
return -1

maximum-swap.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# 670. Maximum Swap
2+
# https://leetcode.com/problems/maximum-swap/
3+
4+
class Solution:
5+
def maximumSwap(self, num: int) -> int:
6+
l = [int(x) for x in str(num)]
7+
max_so_far = (-1, -1,)
8+
candidate = (-1, -1,)
9+
10+
for i in range(len(l)-1, -1, -1):
11+
if l[i] > max_so_far[0]:
12+
max_so_far = (l[i], i,)
13+
elif l[i] < max_so_far[0]:
14+
candidate = (max_so_far[1], i,)
15+
16+
if candidate[0] == -1:
17+
return num
18+
19+
aux = l[candidate[0]]
20+
l[candidate[0]] = l[candidate[1]]
21+
l[candidate[1]] = aux
22+
23+
return int(''.join(map(str, l)))

0 commit comments

Comments
 (0)