立个flag,1-100题每天分配10题,不会就先空着(7,10)。
1. 51:N皇后
class Solution:
def solveNQueens(self, n: int) -> List[List[str]]:
def isValid(n, row, col, path):
for i in range(row):
if path[i][col] == "Q":
return False
for i, j in zip(range(row - 1, -1, -1), range(col - 1, -1, -1)):
if path[i][j] == "Q":
return False
for i, j in zip(range(row - 1, -1, -1), range(col + 1, n)):
if path[i][j] == "Q":
return False
return True
def backtracking(n, row, path, res):
if row == n:
valid = []
for i in path:
valid.append("".join(i))
res.append(valid)
return
for col in range(n):
if isValid(n, row, col, path):
path[row][col] = "Q"
backtracking(n, row + 1, path, res)
path[row][col] = "."
res = []
path = []
for i in range(n):
path.append(["."] * n)
row = 0
backtracking(n, row, path, res)
return res
2. 52:N皇后2
class Solution:
def totalNQueens(self, n: int) -> int:
def isValid(n, row, col, path):
for i in range(row):
if path[i][col] == "Q":
return False
for i, j in zip(range(row - 1, -1, -1), range(col - 1, -1, -1)):
if path[i][j] == "Q":
return False
for i, j in zip(range(row - 1, -1, -1), range(col + 1, n)):
if path[i][j] == "Q":
return False
return True
def backtracking(n, row, path, res):
if row == n:
res.append(True)
return
for col in range(n):
if isValid(n, row, col, path):
path[row][col] = "Q"
backtracking(n, row + 1, path, res)
path[row][col] = "."
res = []
path = []
for i in range(n):
path.append(["."] * n)
row = 0
backtracking(n, row, path, res)
return len(res)
3. 53:最大子数组和
class Solution:
def maxSubArray(self, nums: List[int]) -> int:
dp = [0] * len(nums)
dp[0] = nums[0]
for i in range(1, len(nums)):
if dp[i - 1] > 0:
dp[i] = dp[i - 1] + nums[i]
else:
dp[i] = nums[i]
res = max(dp)
return res
4. 54:螺旋矩阵
class Solution:
def spiralOrder(self, matrix: List[List[int]]) -> List[int]:
res = []
m = len(matrix)
n = len(matrix[0])
left = 0
right = n - 1
top = 0
bottom = m - 1
while True:
for i in range(left, right + 1):
res.append(matrix[top][i])
top = top + 1
if top > bottom:
break
for i in range(top, bottom + 1):
res.append(matrix[i][right])
right = right - 1
if left > right:
break
for i in range(right, left - 1, -1):
res.append(matrix[bottom][i])
bottom = bottom - 1
if top > bottom:
break
for i in range(bottom, top - 1, -1):
res.append(matrix[i][left])
left = left + 1
if left > right:
break
return res
5. 55:跳跃游戏
class Solution:
def canJump(self, nums: List[int]) -> bool:
max_distance = 0
for i in range(len(nums)):
if i <= max_distance:
max_distance = max(max_distance, i + nums[i])
if max_distance >= len(nums) - 1:
return True
else:
return False
6. 56:合并区间
class Solution:
def merge(self, intervals: List[List[int]]) -> List[List[int]]:
res = []
intervals.sort()
interval = intervals[0]
for i in intervals:
if interval[1] < i[0]:
res.append(interval)
interval = i
elif interval[1] >= i[0] and interval[1] < i[1]:
interval = [interval[0], i[1]]
res.append(interval)
return res
7. 57:插入区间
8. 58:最后一个单词的长度
class Solution:
def lengthOfLastWord(self, s: str) -> int:
s = s.strip()
lis = s.split(' ')
return len(lis[-1])
9. 59:螺旋矩阵2
class Solution:
def generateMatrix(self, n: int) -> List[List[int]]:
res = []
for i in range(n):
res.append([0] * n)
left = 0
right = n - 1
top = 0
bottom = n - 1
num = 1
while num <= n**2:
for i in range(left, right + 1):
res[top][i] = num
num = num + 1
top = top + 1
for i in range(top, bottom + 1):
res[i][right] = num
num = num + 1
right = right - 1
for i in range(right, left - 1, -1):
res[bottom][i] = num
num = num + 1
bottom = bottom - 1
for i in range(bottom, top - 1, -1):
res[i][left] = num
num = num + 1
left = left + 1
return res
10. 60:排列序列