提交 06f1e99e 编写于 作者: 每日一练社区's avatar 每日一练社区

增加9习题

上级 d94ddf15
......@@ -21,6 +21,42 @@
</p>
以下程序实现了这一功能,请你填补空白处内容:
```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
......@@ -52,6 +52,40 @@
</div>
</div>
以下程序实现了这一功能,请你填补空白处内容:
```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
......@@ -2,6 +2,50 @@
<p>给定 <em>n</em> 个非负整数表示每个宽度为 1 的柱子的高度图,计算按此排列的柱子,下雨之后能接多少雨水。</p><p> </p><p><strong>示例 1:</strong></p><p><img src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0000-0099/0042.Trapping%20Rain%20Water/images/rainwatertrap.png" style="height: 161px; width: 412px;" /></p><pre><strong>输入:</strong>height = [0,1,0,2,1,0,1,3,2,1,2,1]<strong><br />输出:</strong>6<strong><br />解释:</strong>上面是由数组 [0,1,0,2,1,0,1,3,2,1,2,1] 表示的高度图,在这种情况下,可以接 6 个单位的雨水(蓝色部分表示雨水)。 </pre><p><strong>示例 2:</strong></p><pre><strong>输入:</strong>height = [4,2,0,3,2,5]<strong><br />输出:</strong>9</pre><p> </p><p><strong>提示:</strong></p><ul> <li><code>n == height.length</code></li> <li><code>0 <= n <= 3 * 10<sup>4</sup></code></li> <li><code>0 <= height[i] <= 10<sup>5</sup></code></li></ul>
以下程序实现了这一功能,请你填补空白处内容:
```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
......@@ -2,6 +2,35 @@
<p>给定 <em>n</em> 个非负整数,用来表示柱状图中各个柱子的高度。每个柱子彼此相邻,且宽度为 1 。</p><p>求在该柱状图中,能够勾勒出来的矩形的最大面积。</p><p>&nbsp;</p><p><img src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0000-0099/0084.Largest%20Rectangle%20in%20Histogram/images/histogram.png"></p><p><small>以上是柱状图的示例,其中每个柱子的宽度为 1,给定的高度为&nbsp;<code>[2,1,5,6,2,3]</code>。</small></p><p>&nbsp;</p><p><img src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0000-0099/0084.Largest%20Rectangle%20in%20Histogram/images/histogram_area.png"></p><p><small>图中阴影部分为所能勾勒出的最大矩形面积,其面积为&nbsp;<code>10</code>&nbsp;个单位。</small></p><p>&nbsp;</p><p><strong>示例:</strong></p><pre><strong>输入:</strong> [2,1,5,6,2,3]<strong><br />输出:</strong> 10</pre>
以下程序实现了这一功能,请你填补空白处内容:
```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
......@@ -2,6 +2,27 @@
<p>给你一个未排序的整数数组 <code>nums</code> ,请你找出其中没有出现的最小的正整数。</p><p> </p><p><strong>进阶:</strong>你可以实现时间复杂度为 <code>O(n)</code> 并且只使用常数级别额外空间的解决方案吗?</p><p> </p><p><strong>示例 1:</strong></p><pre><strong>输入:</strong>nums = [1,2,0]<strong><br />输出:</strong>3</pre><p><strong>示例 2:</strong></p><pre><strong>输入:</strong>nums = [3,4,-1,1]<strong><br />输出:</strong>2</pre><p><strong>示例 3:</strong></p><pre><strong>输入:</strong>nums = [7,8,9,11,12]<strong><br />输出:</strong>1</pre><p> </p><p><strong>提示:</strong></p><ul> <li><code>0 <= nums.length <= 300</code></li> <li><code>-2<sup>31</sup> <= nums[i] <= 2<sup>31</sup> - 1</code></li></ul>
以下程序实现了这一功能,请你填补空白处内容:
```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
......@@ -35,6 +35,30 @@
</div>
</div>
以下程序实现了这一功能,请你填补空白处内容:
```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
......@@ -2,6 +2,42 @@
<p>给定一个仅包含 <code>0</code> 和 <code>1</code> 、大小为 <code>rows x cols</code> 的二维二进制矩阵,找出只包含 <code>1</code> 的最大矩形,并返回其面积。</p><p> </p><p><strong>示例 1:</strong></p><img alt="" src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0000-0099/0085.Maximal%20Rectangle/images/maximal.jpg" style="width: 402px; height: 322px;" /><pre><strong>输入:</strong>matrix = [["1","0","1","0","0"],["1","0","1","1","1"],["1","1","1","1","1"],["1","0","0","1","0"]]<strong><br />输出:</strong>6<strong><br />解释:</strong>最大矩形如上图所示。</pre><p><strong>示例 2:</strong></p><pre><strong>输入:</strong>matrix = []<strong><br />输出:</strong>0</pre><p><strong>示例 3:</strong></p><pre><strong>输入:</strong>matrix = [["0"]]<strong><br />输出:</strong>0</pre><p><strong>示例 4:</strong></p><pre><strong>输入:</strong>matrix = [["1"]]<strong><br />输出:</strong>1</pre><p><strong>示例 5:</strong></p><pre><strong>输入:</strong>matrix = [["0","0"]]<strong><br />输出:</strong>0</pre><p> </p><p><strong>提示:</strong></p><ul> <li><code>rows == matrix.length</code></li> <li><code>cols == matrix[0].length</code></li> <li><code>0 <= row, cols <= 200</code></li> <li><code>matrix[i][j]</code> 为 <code>'0'</code> 或 <code>'1'</code></li></ul>
以下程序实现了这一功能,请你填补空白处内容:
```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
......@@ -2,6 +2,30 @@
<p>给你两个单词 <code>word1</code> 和 <code>word2</code>,请你计算出将 <code>word1</code> 转换成 <code>word2</code><em> </em>所使用的最少操作数 。</p><p>你可以对一个单词进行如下三种操作:</p><ul> <li>插入一个字符</li> <li>删除一个字符</li> <li>替换一个字符</li></ul><p> </p><p><strong>示例 1:</strong></p><pre><strong>输入:</strong>word1 = "horse", word2 = "ros"<strong><br />输出:</strong>3<strong><br />解释:</strong>horse -> rorse (将 'h' 替换为 'r')rorse -> rose (删除 'r')rose -> ros (删除 'e')</pre><p><strong>示例 2:</strong></p><pre><strong>输入:</strong>word1 = "intention", word2 = "execution"<strong><br />输出:</strong>5<strong><br />解释:</strong>intention -> inention (删除 't')inention -> enention (将 'i' 替换为 'e')enention -> exention (将 'n' 替换为 'x')exention -> exection (将 'n' 替换为 'c')exection -> execution (插入 'u')</pre><p> </p><p><strong>提示:</strong></p><ul> <li><code>0 <= word1.length, word2.length <= 500</code></li> <li><code>word1</code><code>word2</code> 由小写英文字母组成</li></ul>
以下程序实现了这一功能,请你填补空白处内容:
```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
......@@ -64,6 +64,52 @@
</pre>
</div>
以下程序实现了这一功能,请你填补空白处内容:
```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
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册