# 51
class Solution:
def solveNQueens(self, n):
board, ret = [['.'] * n for _ in range(n)], []
self.dfs(board, n, 0, ret)
return ret
def dfs(self, board, n, row, ret):
if row == n:
ret.append(["".join(i) for i in board])
return
for i in range(n):
if not self.canPlace(row, i, n, board):
continue
board[row][i] = 'Q'
self.dfs(board, n, row + 1, ret)
board[row][i] = '.'
def canPlace(self, row, col, n, board):
for i in range(1, row + 1):
if board[row - i][col] == 'Q':
return False
if col - i >= 0 and board[row - i][col - i] == 'Q':
return False
if col + i < n and board[row - i][col + i] == 'Q':
return False
return True
# 52 跟51一样
class Solution:
def totalNQueens(self, n: int) -> int:
board, ret = [['.'] * n for _ in range(n)], []
self.dfs(board, n, 0, ret)
return len(ret)
def dfs(self, board, n, row, ret):
if row == n:
ret.append(["".join(i) for i in board])
return
for i in range(n):
if not self.canPlace(row, i, n, board):
continue
board[row][i] = 'Q'
self.dfs(board, n, row + 1, ret)
board[row][i] = '.'
def canPlace(self, row, col, n, board):
for i in range(1, row + 1):
if board[row - i][col] == 'Q':
return False
if col - i >= 0 and board[row - i][col - i] == 'Q':
return False
if col + i < n and board[row - i][col + i] == 'Q':
return False
return True
# 53
class Solution:
def maxSubArray(self, nums):
if len(nums) == 1:
return nums[0]
n = len(nums)
l , r = 0 , 0
res = float('-inf')
tmp = 0
while r < n:
tmp += nums[r]
res = max(tmp , res)
if tmp < 0:
l += 1
tmp = 0
r += 1
return res
# 54
class Solution:
def spiralOrder(self, matrix: List[List[int]]) -> List[int]:
rel, i, j, di, dj = [], 0, 0, 0, 1
if matrix != []:
for _ in range(len(matrix) * len(matrix[0])):
rel.append(matrix[i][j])
matrix[i][j] = 0
if matrix[(i + di) % len(matrix)][(j + dj) % len(matrix[0])] == 0:
di, dj = dj, -di
i += di
j += dj
return rel
# 55
class Solution:
def canJump(self, nums):
maxpos = 0
for i in range(len(nums) - 1):
maxpos = max(maxpos , i + nums[i])
if maxpos == i:
return False
return True
# 56
class Solution:
def merge(self, intervals):
if len(intervals) <= 1:
return intervals
res = []
intervals.sort()
for i in intervals:
if not res or res[-1][1]
# 57 一模一样
class Solution:
def insert(self, intervals: List[List[int]], newInterval: List[int]) -> List[List[int]]:
res = []
intervals.append(newInterval)
intervals.sort()
for i in intervals:
if not res or res[-1][1]
# 58
class Solution:
def lengthOfLastWord(self, s):
if len(s) == 0:
return 0
t = s.split(' ')
for i in t[::-1]:
if i != '':
return len(i)
return 0
# 59
class Solution:
def generateMatrix(self,n):
l, r, t, b = 0, n - 1, 0, n - 1
mat = [[0 for _ in range(n)] for _ in range(n)]
num, tar = 1, n * n
while num <= tar:
for i in range(l, r + 1): # left to right
mat[t][i] = num
num += 1
t += 1
for i in range(t, b + 1): # top to bottom
mat[i][r] = num
num += 1
r -= 1
for i in range(r, l - 1, -1): # right to left
mat[b][i] = num
num += 1
b -= 1
for i in range(b, t - 1, -1): # bottom to top
mat[i][l] = num
num += 1
l += 1
return mat
# 60
class Solution:
def getPermutation(self, n, k):
mul_list = [1]
for i in range(1, n):
mul_list.append(mul_list[-1] * i)
ans = []
mod = k-1
nums = list(range(1, n+1))
for mul in mul_list[::-1]:
div, mod = divmod(mod, mul)
ans.append(str(nums.pop(div)))
return "".join(ans)