From 06f1e99eb668aa8e8fd0c0764dd2a52332b3db3f Mon Sep 17 00:00:00 2001 From: zhangzc Date: Wed, 22 Dec 2021 14:37:57 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A09=E4=B9=A0=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../3.python/1.exercises/solution.md" | 48 ++++++++++- .../3.python/2.exercises/solution.md" | 54 +++++++++++- .../3.python/3.exercises/solution.md" | 52 +++++++++++- .../3.python/4.exercises/solution.md" | 36 +++++++- .../3.python/5.exercises/solution.md" | 33 +++++++- .../3.python/6.exercises/solution.md" | 40 ++++++++- .../3.python/7.exercises/solution.md" | 60 +++++++++++++- .../3.python/8.exercises/solution.md" | 32 +++++++- .../3.python/9.exercises/solution.md" | 82 ++++++++++++++++++- 9 files changed, 402 insertions(+), 35 deletions(-) diff --git "a/data/3.dailycode\351\253\230\351\230\266/3.python/1.exercises/solution.md" "b/data/3.dailycode\351\253\230\351\230\266/3.python/1.exercises/solution.md" index 15d297ba8..b5099ab90 100644 --- "a/data/3.dailycode\351\253\230\351\230\266/3.python/1.exercises/solution.md" +++ "b/data/3.dailycode\351\253\230\351\230\266/3.python/1.exercises/solution.md" @@ -21,6 +21,42 @@

+以下程序实现了这一功能,请你填补空白处内容: + +```python +def maze(m, n, route, pos, export): + """走迷宫 + m - 迷宫数组,列表 + n - 迷宫阶数 + route - 可能的路线,列表 + pos - 当前位置,元组 + export - 出口位置,元组 + """ + route.append(pos) + if pos == export: + print(route) + if pos[0] > 0 and m[pos[0]-1][pos[1]] == 0 and (pos[0]-1,pos[1]) not in route: + maze(m, n, route[:], (pos[0]-1,pos[1]), export) + if pos[0] < n-1 and m[pos[0]+1][pos[1]] == 0 and (pos[0]+1,pos[1]) not in route: + maze(m, n, route[:], (pos[0]+1,pos[1]), export) + if pos[1] > 0 and m[pos[0]][pos[1]-1] == 0 and (pos[0],pos[1]-1) not in route: + maze(m, n, route[:], (pos[0],pos[1]-1), export) + ________________________________ +m = [ + [1,1,1,0,1,1,1,1,1,1], + [1,0,0,0,0,0,0,0,1,1], + [1,0,1,1,1,1,1,0,0,1], + [1,0,1,0,0,0,0,1,0,1], + [1,0,1,0,1,1,0,0,0,1], + [1,0,0,1,1,0,1,0,1,1], + [1,1,1,1,0,0,0,0,1,1], + [1,0,0,0,0,1,1,1,0,0], + [1,0,1,1,0,0,0,0,0,1], + [1,1,1,1,1,1,1,1,1,1] +] +maze(m, len(m), list(), (0,3), (7,9)) +``` + ## template ```python @@ -61,7 +97,8 @@ maze(m, len(m), list(), (0,3), (7,9)) ## 答案 ```python - +if pos[1] < n-1 and m[pos[0]][pos[1]+1] == 0 and (pos[0],pos[1]+1) not in route: + maze(m, n, route[:], (pos[0],pos[1]+1), export) ``` ## 选项 @@ -69,17 +106,20 @@ maze(m, len(m), list(), (0,3), (7,9)) ### A ```python - +if pos[1] < n-1 and m[pos[0]][pos[1]+1] == 0 and (pos[0],pos[1]+1) not in route: + maze(m, n, route[:], (pos[0],pos[1]), export) ``` ### B ```python - +if pos[1] < n-1 and m[pos[0]][pos[1]+1] == 0 and (pos[0],pos[1]+1) not in route: + maze(m, n, route[:], (pos[0],pos[1]-1), export) ``` ### C ```python - +if pos[1] < n-1 and m[pos[0]][pos[1]+1] == 0 and (pos[0],pos[1]+1) not in route: + maze(m, n, route[:], (pos[0],pos[0]+1), export) ``` \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/3.python/2.exercises/solution.md" "b/data/3.dailycode\351\253\230\351\230\266/3.python/2.exercises/solution.md" index 29d0a1f43..98d200557 100644 --- "a/data/3.dailycode\351\253\230\351\230\266/3.python/2.exercises/solution.md" +++ "b/data/3.dailycode\351\253\230\351\230\266/3.python/2.exercises/solution.md" @@ -52,6 +52,40 @@ +以下程序实现了这一功能,请你填补空白处内容: + +```python +class Solution(object): + def solveSudoku(self, board): + def isvaild(i,j): + for m in range(9): + if m!=i and board[m][j]==board[i][j]: + return False + for n in range(9): + if n!=j and board[i][n]==board[i][j]: + return False + ______________________ + return True + def f(a,b,board): + for i in range(a,9): + for j in range(b,9): + if board[i][j]=='.': + for c in '123456789': + board[i][j]=c + if isvaild(i,j): + if f(a,b,board):return True + else: board[i][j] = '.' + else:board[i][j] = '.' + return False + return True + f(0,0,board) + return board +# %% +s = Solution() +board = [["5","3",".",".","7",".",".",".","."],["6",".",".","1","9","5",".",".","."],[".","9","8",".",".",".",".","6","."],["8",".",".",".","6",".",".",".","3"],["4",".",".","8",".","3",".",".","1"],["7",".",".",".","2",".",".",".","6"],[".","6",".",".",".",".","2","8","."],[".",".",".","4","1","9",".",".","5"],[".",".",".",".","8",".",".","7","9"]] +print(s.solveSudoku(board)) +``` + ## template ```python @@ -92,7 +126,10 @@ print(s.solveSudoku(board)) ## 答案 ```python - +for m in range(i//3*3,i//3*3+3): + for n in range(j//3*3,j//3*3+3): + if m!=i and n!=j and board[m][n]==board[i][j]: + return False ``` ## 选项 @@ -100,17 +137,26 @@ print(s.solveSudoku(board)) ### A ```python - +for m in range(i//3*3,i//3*3+3): + for n in range(j//3*3,j//3*3+3): + if board[m][n]==board[i][j]: + return False ``` ### B ```python - +for m in range(i//3*3,i//3*3+3): + for n in range(j//3*3,j//3*3+3): + if m==i and n==j and board[m][n]==board[i][j]: + return False ``` ### C ```python - +for m in range(i//3*3,i//3*3+3): + for n in range(j//3*3,j//3*3+3): + if board[m][n]==board[i][j]: + return True ``` \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/3.python/3.exercises/solution.md" "b/data/3.dailycode\351\253\230\351\230\266/3.python/3.exercises/solution.md" index cafeb5a2e..88532a2b9 100644 --- "a/data/3.dailycode\351\253\230\351\230\266/3.python/3.exercises/solution.md" +++ "b/data/3.dailycode\351\253\230\351\230\266/3.python/3.exercises/solution.md" @@ -2,6 +2,50 @@

给定 n 个非负整数表示每个宽度为 1 的柱子的高度图,计算按此排列的柱子,下雨之后能接多少雨水。

 

示例 1:

输入:height = [0,1,0,2,1,0,1,3,2,1,2,1]
输出:
6
解释:
上面是由数组 [0,1,0,2,1,0,1,3,2,1,2,1] 表示的高度图,在这种情况下,可以接 6 个单位的雨水(蓝色部分表示雨水)。

示例 2:

输入:height = [4,2,0,3,2,5]
输出:
9

 

提示:

+以下程序实现了这一功能,请你填补空白处内容: + +```python +class Solution(object): + def trap(self, height): + ls = len(height) + if ls == 0: + return 0 + res, left = 0, 0 + while left < ls and height[left] == 0: + left += 1 + pos = left + 1 + while pos < ls: + if height[pos] >= height[left]: + res += self.rain_water(height, left, pos) + left = pos + pos += 1 + elif pos == ls - 1: + max_value, max_index = 0, pos + for index in range(left + 1, ls): + if height[index] > max_value: + max_value = height[index] + max_index = index + res += self.rain_water(height, left, max_index) + left = max_index + pos = left + 1 + else: + pos += 1 + return res + def rain_water(self, height, start, end): + if end - start <= 1: + return 0 + min_m = min(height[start], height[end]) + _________________________ + step = 0 + for index in range(start + 1, end): + if height[index] > 0: + step += height[index] + return res - step +if __name__ == '__main__': + s = Solution() + print (s.trap([2,6,3,8,2,7,2,5,0])) +``` + ## template ```python @@ -49,7 +93,7 @@ if __name__ == '__main__': ## 答案 ```python - +res = min_m * (end - start - 1) ``` ## 选项 @@ -57,17 +101,17 @@ if __name__ == '__main__': ### A ```python - +res = min_m * (end - start) / 2 ``` ### B ```python - +res = min_m * (end + start) / 2 ``` ### C ```python - +res = min_m * (end - start) ``` \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/3.python/4.exercises/solution.md" "b/data/3.dailycode\351\253\230\351\230\266/3.python/4.exercises/solution.md" index bfa7b2325..93646f84c 100644 --- "a/data/3.dailycode\351\253\230\351\230\266/3.python/4.exercises/solution.md" +++ "b/data/3.dailycode\351\253\230\351\230\266/3.python/4.exercises/solution.md" @@ -2,6 +2,35 @@

给定 n 个非负整数,用来表示柱状图中各个柱子的高度。每个柱子彼此相邻,且宽度为 1 。

求在该柱状图中,能够勾勒出来的矩形的最大面积。

 

以上是柱状图的示例,其中每个柱子的宽度为 1,给定的高度为 [2,1,5,6,2,3]

 

图中阴影部分为所能勾勒出的最大矩形面积,其面积为 10 个单位。

 

示例:

输入: [2,1,5,6,2,3]
输出:
10
+以下程序实现了这一功能,请你填补空白处内容: + +```python +class Solution(object): + def largestRectangleArea(self, heights): + """ + :type heights: List[int] + :rtype: int + """ + largest_rectangle = 0 + ls = len(heights) + stack = [-1] + top, pos = 0, 0 + for pos in range(ls): + while top > 0 and heights[stack[top]] > heights[pos]: + _____________________________ + top -= 1 + stack.pop() + stack.append(pos) + top += 1 + while top > 0: + largest_rectangle = max(largest_rectangle, heights[stack[top]] * (ls - stack[top - 1] - 1)) + top -= 1 + return largest_rectangle +if __name__ == "__main__": + s = Solution() + print (s.largestRectangleArea([2,1,5,6,2,3])) +``` + ## template ```python @@ -34,7 +63,7 @@ if __name__ == "__main__": ## 答案 ```python - +largest_rectangle = max(largest_rectangle, heights[stack[top]] * (pos - stack[top - 1] - 1)) ``` ## 选项 @@ -42,17 +71,18 @@ if __name__ == "__main__": ### A ```python - +largest_rectangle = max(largest_rectangle, heights[stack[top]] * (pos - stack[top] - 1)) ``` ### B ```python +largest_rectangle = max(largest_rectangle, heights[stack[top]] * (pos - stack[top + 1] - 1)) ``` ### C ```python - +largest_rectangle = max(largest_rectangle, heights[stack[top]] * (pos - stack[top - 1] + 1)) ``` \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/3.python/5.exercises/solution.md" "b/data/3.dailycode\351\253\230\351\230\266/3.python/5.exercises/solution.md" index aad7a29ff..02a432282 100644 --- "a/data/3.dailycode\351\253\230\351\230\266/3.python/5.exercises/solution.md" +++ "b/data/3.dailycode\351\253\230\351\230\266/3.python/5.exercises/solution.md" @@ -2,6 +2,27 @@

给你一个未排序的整数数组 nums ,请你找出其中没有出现的最小的正整数。

 

进阶:你可以实现时间复杂度为 O(n) 并且只使用常数级别额外空间的解决方案吗?

 

示例 1:

输入:nums = [1,2,0]
输出:
3

示例 2:

输入:nums = [3,4,-1,1]
输出:
2

示例 3:

输入:nums = [7,8,9,11,12]
输出:
1

 

提示:

+以下程序实现了这一功能,请你填补空白处内容: + +```python +class Solution(object): + def firstMissingPositive(self, nums): + ls = len(nums) + index = 0 + while index < ls: + if nums[index] <= 0 or nums[index] > ls or nums[nums[index] - 1] == nums[index]: + index += 1 + else: + pos = nums[index] - 1 + nums[index], nums[pos] = nums[pos], nums[index] + res = 0 + _________________________ + return res + 1 +# %% +s = Solution() +print(s.firstMissingPositive(nums = [1,2,0])) +``` + ## template ```python @@ -27,7 +48,8 @@ print(s.firstMissingPositive(nums = [1,2,0])) ## 答案 ```python - +while res < ls and nums[res] == res + 1: + res += 1 ``` ## 选项 @@ -35,17 +57,20 @@ print(s.firstMissingPositive(nums = [1,2,0])) ### A ```python - +while res < ls and nums[res] == res: + res += 1 ``` ### B ```python - +while res <= ls and nums[res] == res: + res += 1 ``` ### C ```python - +while res > ls and nums[res] == res + 1: + res += 1 ``` \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/3.python/6.exercises/solution.md" "b/data/3.dailycode\351\253\230\351\230\266/3.python/6.exercises/solution.md" index b97907f14..e7c666282 100644 --- "a/data/3.dailycode\351\253\230\351\230\266/3.python/6.exercises/solution.md" +++ "b/data/3.dailycode\351\253\230\351\230\266/3.python/6.exercises/solution.md" @@ -35,6 +35,30 @@ +以下程序实现了这一功能,请你填补空白处内容: + +```python +class Solution(object): + def __init__(self): + self.count = 0 + def totalNQueens(self, n): + self.dfs(0, n, 0, 0, 0) + return self.count + def dfs(self, row, n, column, diag, antiDiag): + if row == n: + self.count += 1 + return + for index in range(n): + isColSafe = (1 << index) & column == 0 + isDigSafe = (1 << (n - 1 + row - index)) & diag == 0 + isAntiDiagSafe = (1 << (row + index)) & antiDiag == 0 + if isAntiDiagSafe and isColSafe and isDigSafe: + _________________________ +if __name__ == '__main__': + s = Solution() + print (s.totalNQueens(4)) +``` + ## template ```python @@ -64,7 +88,9 @@ if __name__ == '__main__': ## 答案 ```python - +self.dfs(row + 1, n, (1 << index) | column, + (1 << (n - 1 + row - index)) | diag, + (1 << (row + index)) | antiDiag) ``` ## 选项 @@ -72,17 +98,23 @@ if __name__ == '__main__': ### A ```python - +self.dfs(row + 1, n, (index >> 1) | column, + (1 << (n - 1 + row - index)) | diag, + (1 << (row + index)) | antiDiag) ``` ### B ```python - +self.dfs(row + 1, n, (index >> 1) | column, + (1 << (n + 1 + row - index)) | diag, + (1 << (row + index)) | antiDiag) ``` ### C ```python - +self.dfs(row + 1, n, (1 << index) | column, + (1 << (n + 1 + row - index)) | diag, + (1 << (row + index)) | antiDiag) ``` \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/3.python/7.exercises/solution.md" "b/data/3.dailycode\351\253\230\351\230\266/3.python/7.exercises/solution.md" index 00dcc4e71..f93a8bce1 100644 --- "a/data/3.dailycode\351\253\230\351\230\266/3.python/7.exercises/solution.md" +++ "b/data/3.dailycode\351\253\230\351\230\266/3.python/7.exercises/solution.md" @@ -2,6 +2,42 @@

给定一个仅包含 01 、大小为 rows x cols 的二维二进制矩阵,找出只包含 1 的最大矩形,并返回其面积。

 

示例 1:

输入:matrix = [["1","0","1","0","0"],["1","0","1","1","1"],["1","1","1","1","1"],["1","0","0","1","0"]]
输出:
6
解释:
最大矩形如上图所示。

示例 2:

输入:matrix = []
输出:
0

示例 3:

输入:matrix = [["0"]]
输出:
0

示例 4:

输入:matrix = [["1"]]
输出:
1

示例 5:

输入:matrix = [["0","0"]]
输出:
0

 

提示:

+以下程序实现了这一功能,请你填补空白处内容: + +```python +class Solution(object): + def maximalRectangle(self, matrix): + """ + :type matrix: List[List[str]] + :rtype: int + """ + if matrix is None or len(matrix) == 0: + return 0 + ls_row, ls_col = len(matrix), len(matrix[0]) + left, right, height = [0] * ls_col, [ls_col] * ls_col, [0] * ls_col + maxA = 0 + for i in range(ls_row): + curr_left, curr_right = 0, ls_col + for j in range(ls_col): + if matrix[i][j] == '1': + height[j] += 1 + else: + height[j] = 0 + for j in range(ls_col): + if matrix[i][j] == '1': + left[j] = max(left[j], curr_left) + else: + left[j], curr_left = 0, j + 1 + _______________________ + for j in range(ls_col): + maxA = max(maxA, (right[j] - left[j]) * height[j]) + return maxA +# %% +s = Solution() +matrix = [["1","0","1","0","0"],["1","0","1","1","1"],["1","1","1","1","1"],["1","0","0","1","0"]] +print(s.maximalRectangle(matrix)) +``` + ## template ```python @@ -45,7 +81,11 @@ print(s.maximalRectangle(matrix)) ## 答案 ```python - +for j in range(ls_col - 1, -1, -1): + if matrix[i][j] == '1': + right[j] = min(right[j], curr_right) + else: + right[j], curr_right = ls_col, j ``` ## 选项 @@ -53,17 +93,29 @@ print(s.maximalRectangle(matrix)) ### A ```python - +for j in range(ls_col - 1, -1, -1): + if matrix[i][j] == '1': + right[j], curr_right = ls_col, j + else: + right[j] = min(right[j], curr_right) ``` ### B ```python - +for j in range(ls_col - 1, -1, -1): + if matrix[i][j] == '1': + right[j], curr_right = ls_col, j + else: + right[j] = max(right[j], curr_right) ``` ### C ```python - +for j in range(ls_col - 1, -1, -1): + if matrix[i][j] == '1': + right[j] = max(right[j], curr_right) + else: + right[j], curr_right = ls_col, j ``` \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/3.python/8.exercises/solution.md" "b/data/3.dailycode\351\253\230\351\230\266/3.python/8.exercises/solution.md" index 71fbbf262..c749340a9 100644 --- "a/data/3.dailycode\351\253\230\351\230\266/3.python/8.exercises/solution.md" +++ "b/data/3.dailycode\351\253\230\351\230\266/3.python/8.exercises/solution.md" @@ -2,6 +2,30 @@

给你两个单词 word1 和 word2,请你计算出将 word1 转换成 word2 所使用的最少操作数 。

你可以对一个单词进行如下三种操作:

 

示例 1:

输入:word1 = "horse", word2 = "ros"
输出:
3
解释:
horse -> rorse (将 'h' 替换为 'r')rorse -> rose (删除 'r')rose -> ros (删除 'e')

示例 2:

输入:word1 = "intention", word2 = "execution"
输出:
5
解释:
intention -> inention (删除 't')inention -> enention (将 'i' 替换为 'e')enention -> exention (将 'n' 替换为 'x')exention -> exection (将 'n' 替换为 'c')exection -> execution (插入 'u')

 

提示:

+以下程序实现了这一功能,请你填补空白处内容: + +```python +class Solution(object): + def minDistance(self, word1, word2): + ls_1, ls_2 = len(word1), len(word2) + dp = list(range(ls_1 + 1)) + for j in range(1, ls_2 + 1): + pre = dp[0] + dp[0] = j + for i in range(1, ls_1 + 1): + temp = dp[i] + if word1[i - 1] == word2[j - 1]: + dp[i] = pre + else: + ____________________________ + pre = temp + return dp[ls_1] +if __name__ == '__main__': + s = Solution() + print (s.minDistance("horse","ros")) + print (s.minDistance("intention","execution")) +``` + ## template ```python @@ -29,7 +53,7 @@ if __name__ == '__main__': ## 答案 ```python - +dp[i] = min(pre + 1, dp[i] + 1, dp[i - 1] + 1) ``` ## 选项 @@ -37,17 +61,17 @@ if __name__ == '__main__': ### A ```python - +dp[i] = min(pre + 1, dp[i] + 1, dp[i + 1] + 1) ``` ### B ```python - +dp[i] = min(pre + 1, dp[i] + 1, dp[i - 1] - 1) ``` ### C ```python - +dp[i] = min(pre + 1, dp[i] + 1, dp[i + 1] - 1) ``` \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/3.python/9.exercises/solution.md" "b/data/3.dailycode\351\253\230\351\230\266/3.python/9.exercises/solution.md" index 3bdbb7f3f..ae4b7da8b 100644 --- "a/data/3.dailycode\351\253\230\351\230\266/3.python/9.exercises/solution.md" +++ "b/data/3.dailycode\351\253\230\351\230\266/3.python/9.exercises/solution.md" @@ -64,6 +64,52 @@ +以下程序实现了这一功能,请你填补空白处内容: + +```python +class Solution(object): + def fullJustify(self, words, maxWidth): + """ + :type words: List[str] + :type maxWidth: int + :rtype: List[str] + """ + res = [] + res_list = [] + curr = [] + count, pos = 0, 0 + while pos < len(words): + word = words[pos] + if len(word) > maxWidth: + pos += 1 + if len(word) + count + len(curr)<= maxWidth: + count += len(word) + curr.append(word) + pos += 1 + else: + res_list.append(curr) + curr = [] + count = 0 + if len(curr) > 0: + res_list.append(curr) + for index, curr in enumerate(res_list): + text = '' + remain = sum([len(t) for t in curr]) + if len(curr) == 1: + text = curr[0] + ' ' * (maxWidth - remain) + elif index == len(res_list) - 1: + text = ' '.join(curr) + text += ' ' * (maxWidth - remain - len(curr) + 1) + else: + ___________________________ + res.append(text) + return res +if __name__ == '__main__': + s = Solution() + print (s.fullJustify(["Don't","go","around","saying","the","world","owes","you","a","living;","the","world","owes","you","nothing;","it","was","here","first."],30)) +``` + + ## template ```python @@ -119,7 +165,14 @@ if __name__ == '__main__': ## 答案 ```python - +step = (maxWidth - remain) / (len(curr) - 1 ) +extra = (maxWidth - remain) % (len(curr) - 1 ) +for index in range(len(curr) - 1): + text += curr[index] + ' ' * int(step) + if extra > 0: + text += ' ' + extra -= 1 +text += curr[-1] ``` ## 选项 @@ -127,17 +180,38 @@ if __name__ == '__main__': ### A ```python - +step = (maxWidth - remain) / (len(curr) - 1 ) +extra = (maxWidth - remain) % (len(curr) - 1 ) +for index in range(len(curr) - 1): + text += curr[index] + ' ' * int(step) + if extra > 0: + text += ' ' + extra += 1 +text += curr[-1] ``` ### B ```python - +step = (maxWidth - remain) / (len(curr) - 1 ) +extra = (maxWidth - remain) % (len(curr) - 1 ) +for index in range(len(curr) - 1): + text += curr[index] + ' ' * int(step) + if extra > 0: + text += ' ' + extra -= 1 +text += curr[1] ``` ### C ```python - +step = (maxWidth - remain) / (len(curr) - 1 ) +extra = (maxWidth - remain) % (len(curr) - 1 ) +for index in range(len(curr) - 1): + text += curr[index] + ' ' * int(step) + if extra > 0: + text += ' ' + extra += 1 +text += curr[1] ``` \ No newline at end of file -- GitLab