30 days in Leetcode (Basic Algorithm)
First day of Basic Algorithm training in Leetcode.
Two Sum
1 | class Solution: |
2nd day of Basic Algorithm training in Leetcode.
Remove-duplicates-from-sorted-array1
2
3
4
5
6
7
8
9
10class Solution:
def removeDuplicates(self, nums: List[int]) -> int:
_ = len(nums)
if _ < 2: return _
i = 0
for j xrange(1,_):
if nums[j] != nums[i]:
i += 1
nums[i] = nums[j]
return i+1
3rd day of Basic Algorithm training in Leetcode.
Remove-element1
2
3
4class Solution:
def removeElement(self, nums: List[int], val: int) -> int:
while val in nums: nums.remove(val)
return len(nums)
4th day of Basic Algorithm training in Leetcode.
3sum1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25class Solution:
def threeSum(self, nums: List[int]) -> List[List[int]]:
l = len(nums)
if l < 3: return []
nums.sort()
res = []
for pt in range(l - 2):
if nums[pt] > 0: break
if pt > 0 and nums[pt] == nums[pt - 1]: continue
lf, rg = pt + 1, l - 1
while lf < rg:
_ = nums[lf] + nums[rg] + nums[pt]
if _ < 0:
lf += 1
elif _ > 0:
rg -= 1
else:
res.append([nums[lf], nums[rg], nums[pt]])
while lf < rg and nums[lf] == nums[lf + 1]:
lf += 1
while lf < rg and nums[rg] == nums[rg - 1]:
rg -= 1
lf += 1
rg -= 1
return(res)
5th day of Basic Algorithm training in Leetcode.
3sum closest1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24class Solution:
def threeSumClosest(self, nums: [int], target: int) -> int:
l = len(nums)
if l < 3: return []
nums.sort()
res = []
lastsum = nums[0] + nums[1] + nums[-1]
_ = lastsum
for pt in range(l - 2):
lf, rg = pt + 1, l - 1
if pt > 0 and nums[pt] == nums[pt - 1]: continue
while lf < rg:
_ = nums[lf] + nums[rg] + nums[pt]
if _ == target:
return target
if abs(lastsum - target) > abs(_ - target):
lastsum = _
if _ < target:
lf += 1
elif _ > target:
rg -= 1
return lastsum
6th day of Basic Algorithm training in Leetcode.
Best Time to Buy and Sell Stock III1
2
3
4
5
6
7
8
9
10
11
12from sys import maxsize
class Solution:
def maxProfit(self, prices: List[int]) -> int:
if not prices: return 0
s1 = s2 = 0
b1 = b2 = -maxsize
for e in prices:
s2 = max(s2, b2 + e)
b2 = max(b2, s1 - e)
s1 = max(s1, b1 + e)
b1 = max(b1, -e)
return s2
7th day of Basic Algorithm training in Leetcode.
Merge Two Sorted Lists
Solution 11
2
3
4
5
6
7
8
9
10
11
12
13# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution:
def mergeTwoLists(self, l1: ListNode, l2: ListNode) -> ListNode:
if l1 and l2:
if l1.val > l2.val:
l1, l2 = l2, l1
l1.next = self.mergeTwoLists(l1.next, l2)
return l1 or l2
Solution 21
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18class Solution:
def mergeTwoLists(self, l1: ListNode, l2: ListNode) -> ListNode:
if None in (l1, l2):
return l1 or l2
n0 = cur = ListNode(None)
n0.next = l1
while l1 and l2:
if l1.val < l2.val:
l1 = l1.next
else:
nxt = cur.next
cur.next = l2
tmp = l2.next
l2.next = nxt
l2 = tmp
cur = cur.next
cur.next = l1 or l2
return n0.next
8th day of Basic Algorithm training in Leetcode.
Remove Duplicates from Sorted List
1 | # Definition for singly-linked list. |
9th day of Basic Algorithm training in Leetcode.
Linked List Cycle
1 | # Definition for singly-linked list. |
10th day of Basic Algorithm training in Leetcode.
Add Two Numbers
1 | # Definition for singly-linked list. |
11th day of Basic Algorithm training in Leetcode.
Remove Nth Node From End of List
1 | # Definition for singly-linked list. |
12th day of Basic Algorithm training in Leetcode.
Merge k Sorted Lists
1 | # Definition for singly-linked list. |
13th day of Basic Algorithm training in Leetcode.
Roman to Integer
Solution 1: Use RegularExpression module1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28import re
reg = re.compile(r'IV|IX|XL|XC|CD|CM')
class Solution:
romanValL = {
'I':1,
'V':5,
'X':10,
'L':50,
'C':100,
'D':500,
'M':1000,
}
romanValH = {
'IV':4,
'IX':9,
'XL':40,
'XC':90,
'CD':400,
'CM':900,
}
def romanToInt(self, s: str) -> int:
val = 0
for x in reg.finditer(s):
val += self.romanValH[x[0]]
if val: s = re.sub(r'IV|IX|XL|XC|CD|CM', '', s)
for x in s:
val += self.romanValL[x]
return val
Solution 2: Use plain replace method1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29class Solution:
romanValL = {
'I':1,
'V':5,
'X':10,
'L':50,
'C':100,
'D':500,
'M':1000,
}
romanValH = {
'IV':4,
'IX':9,
'XL':40,
'XC':90,
'CD':400,
'CM':900,
}
def romanToInt(self, s: str) -> int:
val = 0
for x,v in self.romanValH.items():
while x in s:
val += v
s = s.replace(x,'',1)
for x,v in self.romanValL.items():
while x in s:
val += v
s = s.replace(x,'',1)
return val
14th day of Basic Algorithm training in Leetcode.
Longest Common Prefix
1 | class Solution: |
15th day of Basic Algorithm training in Leetcode.
Valid Parentheses
1 | class Solution: |
16th day of Basic Algorithm training in Leetcode.
Longest Substring Without Repeating Characters
1 | class Solution: |
17th day of Basic Algorithm training in Leetcode.
Longest Palindromic Substring
1 | class Solution: |
18th day of Basic Algorithm training in Leetcode.
Regular Expression Matching
1 | import re |
19th day of Basic Algorithm training in Leetcode
Same Tree
1 | # Definition for a binary tree node. |
20th day of Basic Algorithm training in Leetcode
Symmetric Tree
1 | # Definition for a binary tree node. |
21th day of Basic Algorithm training in Leetcode
Maximum Depth of Binary Tree
1 | # Definition for a binary tree node. |
22th day of Basic Algorithm training in Leetcode
Binary Tree Level Order Traversal
1 | # Definition for a binary tree node. |
23th day of Basic Algorithm training in Leetcode
Unique Binary Search Trees II
1 | # Definition for a binary tree node. |
24th day of Basic Algorithm training in Leetcode
Recover Binary Search Tree
1 | # Definition for a binary tree node. |
25th day of Basic Algorithm training in Leetcode
Best Time to Buy and Sell Stock II
1 | class Solution: |
26th day of Basic Algorithm training in Leetcode
Is Subsequence
1 | class Solution: |
27th day of Basic Algorithm training in Leetcode
Assign Cookies
1 | class Solution: |
28th day of Basic Algorithm training in Leetcode
Jump Game
1 | class Solution: |
29th day of Basic Algorithm training in Leetcode
Gas Station
1 | class Solution: |
30th day of Basic Algorithm training in Leetcode
Wildcard Matching
1 | class Solution: |