Leetcode 30 训练记录 (基本算法)
把30篇博文合并成一篇,记录 leetcode 的30天训练以及结果
第一天
两数之和
1 | class Solution: |
第二天
已排序列表去重1
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
第三天
删除元素1
2
3
4class Solution:
def removeElement(self, nums: List[int], val: int) -> int:
while val in nums: nums.remove(val)
return len(nums)
第四天
三数之和1
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)
第五天
最近三数之和1
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
第六天
最佳买卖股票时机 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
第七天
合并列表
解法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
解法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
第八天
排序列表去重
1 | # Definition for singly-linked list. |
第九天
链表判环
1 | # Definition for singly-linked list. |
第十天
两数相加
1 | # Definition for singly-linked list. |
第十一天
删除列表末端第N个点
1 | # Definition for singly-linked list. |
第十二天
合并K个有序列表
1 | # Definition for singly-linked list. |
第十三天
罗马数字转十进数
解法1:正则表达式1
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
解法2:替换1
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
第十四天
最长相同前缀
1 | class Solution: |
第十五天
括号匹配
1 | class Solution: |
第十六天
无重复的最长子字符串
1 | class Solution: |
第十七天
最长回文数
1 | class Solution: |
第十八天
正则表达式匹配
1 | import re |
第十九天
相同的树
1 | # Definition for a binary tree node. |
第二十天
对称树
1 | # Definition for a binary tree node. |
第二十一天
二叉树的最大深度
1 | # Definition for a binary tree node. |
第二十二天
正序遍历二叉树
1 | # Definition for a binary tree node. |
第二十三天
Unique Binary Search Trees II
1 | # Definition for a binary tree node. |
第二十四天
恢复二叉搜索树
1 | # Definition for a binary tree node. |
第二十五天
买卖股票的最佳时机 II
1 | class Solution: |
第二十六天
是否存在子序列
1 | class Solution: |
第二十七天
分饼干
1 | class Solution: |
第二十八天
跳一跳
1 | class Solution: |
第二十九天
加气站分配
1 | class Solution: |
第三十天
通配符匹配
1 | class Solution: |