Skip to content

Commit 21f92d2

Browse files
authored
Add files via upload
1 parent d686f9a commit 21f92d2

14 files changed

+279
-0
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# 每次将数组最小值取反替换
2+
class Solution(object):
3+
def largestSumAfterKNegations(self, A, K):
4+
for _ in range(K):
5+
num = min(A)
6+
i = A.index(num)
7+
A[i] = -num
8+
return sum(A)
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# 列表第一个值构造为根结点,比根结点小的值作为左子树,比根结点大的值作为右子树,递归构造结点成二叉树
2+
class Solution(object):
3+
def bstFromPreorder(self, preorder):
4+
if not preorder:
5+
return None
6+
root = TreeNode(preorder[0])
7+
left, right = [], []
8+
for num in preorder[1:]:
9+
if num < preorder[0]:
10+
left.append(num)
11+
else:
12+
right.append(num)
13+
root.left = self.bstFromPreorder(left)
14+
root.right = self.bstFromPreorder(right)
15+
return root
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# 遍历记录左括号和右括号的个数,若相等则凑成一组
2+
class Solution(object):
3+
def removeOuterParentheses(self, S):
4+
l = r = 0
5+
res = cur = ''
6+
for s in S:
7+
if s == '(':
8+
l += 1
9+
if s == ')':
10+
r += 1
11+
cur += s
12+
if l == r:
13+
res += cur[1:-1]
14+
cur = ''
15+
return res
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# 深度优先搜索,得到所有路径表示的数字,最后转为十进制相加
2+
class Solution(object):
3+
def sumRootToLeaf(self, root):
4+
global res
5+
res, sum = [], 0
6+
self.dfs(root, '')
7+
for i in res:
8+
sum += int(i, 2)
9+
return sum
10+
11+
def dfs(self, root, s):
12+
if not root:
13+
return ''
14+
if not root.left and not root.right:
15+
res.append(s + str(root.val))
16+
if root.left:
17+
self.dfs(root.left, s + str(root.val))
18+
if root.right:
19+
self.dfs(root.right, s + str(root.val))

leetcode2/1023.驼峰式匹配.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Solution(object):
2+
def camelMatch(self, queries, pattern):
3+
res = []
4+
l = len(pattern)
5+
for q in queries:
6+
i = 0
7+
flag = True
8+
for char in q:
9+
# 与模板字符匹配
10+
if i < l and char == pattern[i]:
11+
i += 1
12+
# 与模板字符不匹配,且为大写字母,则待查项与模式串不匹配
13+
elif 'A' <= char <= 'Z':
14+
flag = False
15+
break
16+
# 模板没有全部匹配
17+
if i < l:
18+
flag = False
19+
res.append(flag)
20+
return res

leetcode2/1024.视频拼接.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# 贪心,每一步都走可到范围内下一步最远的那一步
2+
class Solution(object):
3+
def videoStitching(self, clips, T):
4+
d = {}
5+
for clip in clips:
6+
# 用字典记录每个起点的最远终点
7+
d[clip[0]] = max(d.get(clip[0], 0), clip[1])
8+
res = 1
9+
start = s = 0
10+
end = e = reach = d.get(0, 0)
11+
while reach < T:
12+
# 在已有范围内搜寻可到最远距离
13+
for cur in range(start + 1, end + 1):
14+
if d.get(cur, 0) > reach:
15+
# 记录下一轮的起点和终点
16+
s, e, reach = cur, d[cur], d[cur]
17+
# 本轮停留在原地
18+
if s == start and e == end:
19+
return -1
20+
start, end = s, e
21+
res += 1
22+
return res

leetcode2/1025.除数博弈.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# 如果黑板上写的是奇数,那么下一个人改完之后肯定会变成偶数,因为奇数没有偶数因子并且奇数减去奇数差为偶数。
2+
# 谁先在黑板上写出3谁就赢了,因为3之后另外一个人只能写2,然后你再写1,另外一个人就没办法写了。
3+
# 所以如果想赢,必须让自己每次写完之后黑板上都是奇数,因为这样对方就只能写偶数,那么3肯定就会是我方写。
4+
class Solution(object):
5+
def divisorGame(self, N):
6+
return N % 2 == 0
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# 递归时计算与当前祖先里面的最大值、最小值的差值,即可算出当前节点的最大差值
2+
# 而一颗树的祖先差值为左子树的祖先差值、右子树的祖先差值、当前节点的祖先差值三者里面的最大值
3+
class Solution(object):
4+
def maxAncestorDiff(self, root):
5+
return self.dfs(root, root.val, root.val)
6+
7+
def dfs(self, root, minVal, maxVal):
8+
if not root:
9+
return 0
10+
val = root.val
11+
root_res = max(abs(minVal - val), abs(maxVal - val))
12+
left_res = self.dfs(root.left, min(val, minVal), max(val, maxVal))
13+
right_res = self.dfs(root.right, min(val, minVal), max(val, maxVal))
14+
return max(root_res, left_res, right_res)

leetcode2/1027.最长等差数列.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# 暴力破解
2+
class Solution(object):
3+
def longestArithSeqLength(self, A):
4+
res = 0
5+
for i in range(len(A)-2):
6+
for j in range(i+1, len(A)-1):
7+
count = 2
8+
diff = A[j] - A[i]
9+
cur = A[j] + diff
10+
for k in range(j+1, len(A)):
11+
if A[k] == cur:
12+
count += 1
13+
cur += diff
14+
res = max(res, count)
15+
return res
16+
17+
18+
# 动态规划
19+
# dp[i][diff]表示以下标为i的元素结尾且公差为diff的等差子序列的最大长度
20+
# 由于使用数组内存开销太大而且公差可能是负数,所以选择使用字典来代替数组
21+
class Solution2(object):
22+
def longestArithSeqLength(self, A):
23+
n = len(A)
24+
dp = [{} for _ in range(n)]
25+
res = 2
26+
for i in range(1, n):
27+
for j in range(0, i):
28+
diff = A[i] - A[j]
29+
dp[i][diff] = dp[j].get(diff, 1) + 1
30+
res = max(res, dp[i][diff])
31+
return res
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
class Solution(object):
2+
def recoverFromPreorder(self, S):
3+
# 遍历字符串,记录结点值和对应层数
4+
layer, num, queue = 0, '', []
5+
for char in S:
6+
if char == '-':
7+
if num:
8+
queue.append((num, layer))
9+
num, layer = '', 0
10+
layer += 1
11+
else:
12+
num += char
13+
if num:
14+
queue.append((num, layer))
15+
16+
head = TreeNode(None)
17+
# 存放已还原的结点和层数
18+
stack = [(head, -1)]
19+
for num, layer in queue:
20+
# 层数小于等于最后一个已还原结点时,弹出已还原结点直到其父节点
21+
while layer <= stack[-1][1]:
22+
stack.pop()
23+
node = TreeNode(num)
24+
parent = stack[-1][0]
25+
stack.append((node, layer))
26+
# 左孩子优先
27+
if parent.left:
28+
parent.right = node
29+
else:
30+
parent.left = node
31+
return head.left
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
class Solution(object):
2+
def numMovesStones(self, a, b, c):
3+
a, b, c = sorted([a, b, c])
4+
if a+1==b and b+1==c:
5+
return [0, 0]
6+
if a+1==b or a+2==b or b+1==c or b+2==c:
7+
return [1, c-a-2]
8+
return [2, c-a-2]

leetcode2/1034.边框着色.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# 连通分量边界:与至少一个非连通分量相邻的连通分量,或网格的首尾行列
2+
class Solution(object):
3+
def colorBorder(self, grid, r0, c0, color):
4+
m = len(grid)
5+
n = len(grid[0])
6+
ret = [[0] * n for _ in range(m)]
7+
vis = [[0] * n for _ in range(m)]
8+
for i in range(m):
9+
for j in range(n):
10+
ret[i][j] = grid[i][j]
11+
# 代表四个方向的分量
12+
ds = ((0,1),(1,0),(0,-1),(-1,0))
13+
t = grid[r0][c0]
14+
15+
def dfs(i, j):
16+
# 判断当前网格是否已搜寻过
17+
if not vis[i][j]:
18+
vis[i][j] = True
19+
edge = False
20+
# 遍历四个方向的网格
21+
for di, dj in ds:
22+
ni, nj = i+di, j+dj
23+
# 某个方向在网格内
24+
if 0<=ni<m and 0<=nj<n:
25+
# 值相等则为连通分量,递归
26+
if grid[ni][nj] == t:
27+
dfs(ni,nj)
28+
# 值不相等则当前网格是连通分量边界
29+
else:
30+
edge = True
31+
# 某个方向不在网格内,则当前网格是网格边界
32+
else:
33+
edge = True
34+
# 遍历完四个方向后判断,若是边界则染色
35+
if edge:
36+
ret[i][j] = color
37+
38+
dfs(r0, c0)
39+
return ret

leetcode2/1035.不相交的线.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# 相当于寻找最长公共子串
2+
class Solution(object):
3+
def maxUncrossedLines(self, A, B):
4+
n, m = len(A), len(B)
5+
dp = [[0]*(n+1) for _ in range(m+1)]
6+
for i in range(m):
7+
for j in range(n):
8+
if A[j] == B[i]:
9+
dp[i+1][j+1] = dp[i][j] + 1
10+
else:
11+
dp[i+1][j+1] = max(dp[i][j+1], dp[i+1][j])
12+
return dp[-1][-1]
13+
14+
# 2 5 1 2 5
15+
#
16+
# 0 0 0 0 0 0
17+
# 10 0 0 0 0 0 0
18+
# 5 0 0 1 1 1 1
19+
# 2 0 1 1 1 2 2
20+
# 1 0 1 1 2 2 2
21+
# 5 0 1 2 2 2 3
22+
# 2 0 1 2 2 3 3

leetcode2/1036.逃离大迷宫.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
class Solution(object):
2+
def isEscapePossible(self, blocked, source, target):
3+
ds = ((1,0), (0,1), (-1,0), (0,-1))
4+
# 元组节省空间
5+
blocks = set(map(tuple, blocked))
6+
T = 999999
7+
8+
def not_circle(p):
9+
cur = [p]
10+
vis = set()
11+
vis.add(tuple(p))
12+
for _ in range(50):
13+
next = list()
14+
# 遍历当前可到达的方格
15+
for i, j in cur:
16+
# 遍历每个方格四个方向的方格
17+
for di, dj in ds:
18+
ni, nj = i+di, j+dj
19+
# 方格在有效范围内,未访问过,且不在封锁列表中
20+
if 0<=ni<=T and 0<=nj<=T and (ni,nj) not in vis and (ni,nj) not in blocks:
21+
# 将该方格添加到下一可访问列表和已访问列表
22+
next.append((ni,nj))
23+
vis.add((ni,nj))
24+
cur = next
25+
# 判断50次遍历搜寻后当前是否还有可访问的方格,若没有则说明封闭
26+
return bool(cur)
27+
28+
# 判断起点和终点是否连通
29+
return not_circle(source) and not_circle(target)

0 commit comments

Comments
 (0)