Because my recent illness is cervical spondylosis, which I hate, and my back is numb, so, alas, a difficult life.

129: sum of root node and leaf node

Simple questions

Before traversal, when traversing down, pass in the current path and remember to multiply each layer by 10

class Solution: def sumNumbers(self, root: TreeNode) -> int: rel = 0 def preorder(root,num): nonlocal rel if (not root.left) and (not root.right): rel += (root.val + 10*num) if root.left: preorder(root.left,10*num + root.val) if root.right: preorder(root.right,10*num + root.val) preorder(root,0) return rel

130: surrounded area

I had written it right long ago, but I wrote all O as zero drunk

First design an access array called VISITED

The method is to look at the 'o' on the four sides, access all connected O's from these O's, and set their access flag to 1

Finally, traverse the whole table. If 'O' has not been accessed, it will be changed to X

m = len(board) n = len(board[0]) visited = [[0]* n for i in range(m)] def dfs(i,j): visited[i][j] = 1 if i >0 and board[i-1][j] == 'O' and not visited[i-1][j]: dfs(i-1,j) if i < m-1 and board[i+1][j] == 'O' and not visited[i+1][j]: dfs(i+1,j) if j >0 and board[i][j-1] == 'O' and not visited[i][j-1]: dfs(i,j-1) if j < n-1 and board[i][j+1]== 'O' and not visited[i][j+1]: dfs(i,j+1) for j in range(n): if board[0][j] == 'O' and not visited[0][j]: dfs(0,j) if board[m-1][j] == 'O' and not visited[m-1][j]: dfs(m-1,j) for i in range(m): if board[i][0] == 'O' and not visited[i][0]: dfs(i,0) if board[i][n-1] == 'O' and not visited[i][n-1]: dfs(i,n-1) for i in range(1,m-1): for j in range(1,n-1): if board[i][j] == 'O' and not visited[i][j]: board[i][j] = 'X' Author: yizhu-jia Link: https://leetcode-cn.com/problems/surrounded-regions/solution/0-he-o-sha-sha-fen-bu-qing-chu-by-yizhu-oemoa/ Source: force buckle( LeetCode) The copyright belongs to the author. For commercial reprint, please contact the author for authorization, and for non-commercial reprint, please indicate the source.

131: split palindrome string

The first question is my own way. Hey, I'm so happy.

First, traverse S once to make a palind string list, which is more complex

palind[i] stores palindrome strings starting with I

palind[i][0] stores all the same palindrome strings starting with I, such as' SSS', 'Ss','s'

palind[i][1] stores palindromes with different letters starting with I, such as' abba '

What is stored is not the palindrome string itself, but the beginning and end of the palindrome string. For example, [0,1] represents a single letter of s[0].

How to make a palindrome string list?

Make the last letter first. There is only one s [n-1: n] that starts with him

For other letters, first see if they can form the same letter palindrome string, which is palind[i+1][0]

If he is the same as the last letter of all palindromes in palind[i+1], he can add the following letter to form a new palindrome string. Be careful not to go beyond the end

In this way, the palindrome string list described above is obtained at bit 0

Then there is a simple backtracking

For each palind string in palind[0], if the end is not n, continue backtracking from that end. If n is added to the result at the end

class Solution: def partition(self, s: str) -> List[List[str]]: n = len(s) palind = [[[],[]] for i in range(n)] palind[n-1] = [[[n-1,n]],[]] for i in range(n-2,-1,-1): palind[i][0].append([i,i+1]) for each in palind[i + 1][0]: if s[i] == s[i+1]: palind[i][0].append([i,each[1]]) elif each[1] != n and s[i] == s[each[1]] : palind[i][1] .append([i,each[1]+1]) for each in palind[i+1][1]: if each[1] != n and s[i] == s[each[1]] : palind[i][1] .append([i,each[1]+1]) #After the operation, palind[i] stores all palindromes starting with I. rel = [] def dfs(start,curpath): for each in palind[start][0] + palind[start][1]: curpath.append(s[each[0]:each[1]]) if each[1] != n: dfs(each[1],curpath) else: rel.append(curpath[:]) curpath.pop() dfs(0,[]) return rel Author: yizhu-jia Link: https://leetcode-cn.com/problems/palindrome-partitioning/solution/rang-wo-po-you-cheng-jiu-gan-de-yi-dao-t-6efh/ Source: force buckle( LeetCode) The copyright belongs to the author. For commercial reprint, please contact the author for authorization, and for non-commercial reprint, please indicate the source.

132 ， palindrome string 2 ， incarnate as CV Engineer ， unhappy

This question is too difficult.

class Solution: def minCut(self, s: str) -> int: n = len(s) g = [[True] * n for _ in range(n)] for i in range(n - 1, -1, -1): for j in range(i + 1, n): g[i][j] = (s[i] == s[j]) and g[i + 1][j - 1] f = [float("inf")] * n for i in range(n): if g[0][i]: f[i] = 0 else: for j in range(i): if g[j + 1][i]: f[i] = min(f[i], f[j] + 1) return f[n - 1] Author: yizhu-jia Link: https://leetcode-cn.com/problems/palindrome-partitioning-ii/solution/cla-la-la-la-la-la-wo-shi-c-vgong-cheng-m1ekj/ Source: force buckle( LeetCode) The copyright belongs to the author. For commercial reprint, please contact the author for authorization, and for non-commercial reprint, please indicate the source.

133: Clone graph

Created a lot of auxiliary variables

The original cloning problem only needs to establish the dictionary mapping between the original node and the new node

""" # Definition for a Node. class Node: def __init__(self, val = 0, neighbors = None): self.val = val self.neighbors = neighbors if neighbors is not None else [] """ class Solution: def cloneGraph(self, node: 'Node') -> 'Node': if not node: return node QUEUE = deque() nodedict = {} cnode = [] i = 0 QUEUE.append(node) while QUEUE: curNODE = QUEUE.popleft() newnode = Node(val=curNODE.val) cnode.append(newnode) nodedict[curNODE] = i i+= 1 for each in curNODE.neighbors: if each in nodedict: newnode.neighbors.append(cnode[nodedict[each]]) cnode[nodedict[each]].neighbors.append(newnode) elif each not in QUEUE: QUEUE.append(each) return cnode[0] Author: yizhu-jia Link: https://leetcode-cn.com/problems/clone-graph/solution/chuang-jian-liao-yi-da-dui-de-fu-zhu-bia-0l7m/ Source: force buckle( LeetCode) The copyright belongs to the author. For commercial reprint, please contact the author for authorization, and for non-commercial reprint, please indicate the source.

134: gas station problem

The idea is to find the maximum segment sum

If the oil and exceed the consumption and instructions, it can go through, otherwise it can't

When you can get through, it's a simple question to find the sum of which paragraph

CURSUM = refuel later - start again when the consumption is less than 0

The answer is not quite the same as my thinking, but to consider whether it can go through.

class Solution: def canCompleteCircuit(self, gas: List[int], cost: List[int]) -> int: if sum(gas) < sum (cost): return -1 n = len(gas) temp = [gas[i] - cost[i] for i in range(n)] cursum = 0 maxsum = -1 curindex = 0 for index in range(2*n): cursum += temp[index%n] if cursum< 0: cursum = 0 curindex = index+1 if cursum > maxsum: maxsum = cursum maxindex = curindex return maxindex Author: yizhu-jia Link: https://leetcode-cn.com/problems/gas-station/solution/ahahahahyi-ci-ti-jiao-jiu-guo-liao-kai-x-gzb2/ Source: force buckle( LeetCode) The copyright belongs to the author. For commercial reprint, please contact the author for authorization, and for non-commercial reprint, please indicate the source.