题51(困难):
分析:
递归金典题:N皇后问题,搞过扫雷游戏就很简单了
python代码:
python
class Solution:
def solveNQueens(self, n: int) -> List[List[str]]:
#n 皇后问题--》非线性--》选递归,
res=[]
queue_count=n
#设置皇后,递归函数,传当前可行位置+放置第几个
def set_queue(free_queue,queue_board,n):
if n<=0 or n>queue_count:
return
if n==queue_count:#放置第九个皇后
if 1 not in free_queue[n-1]:
return
else:
for i in range(queue_count):
if free_queue[n-1][i]==1:
queue_board[n-1][i]='Q'
res.append([''.join(i.copy()) for i in queue_board])
queue_board[n-1][i]='.'
return
else:
for i in range(queue_count):
if free_queue[n-1][i]==1:
free_queue_new = [i.copy() for i in free_queue]
queue_board_new = [i.copy() for i in queue_board]
queue_board_new[n-1][i]='Q'
for m in range(queue_count-n):
#设置纵列为0
free_queue_new[n+m][i]=0
#设置横列为0(要不要无所谓)
if i+m+1<queue_count:
free_queue_new[n-1][i+m+1]=0
#设置斜率为1的列为0
if n+m<queue_count and i+m+1<queue_count:
free_queue_new[n+m][i+m+1]=0
#设置斜率为-1的列为0
if n+m<queue_count and i-m-1>=0:
free_queue_new[n+m][i-m-1]=0
set_queue(free_queue_new,queue_board_new,n+1)
return
free_queue=[[1 for i in range(queue_count)] for j in range(queue_count)]
queue_board=[['.' for i in range(queue_count)] for j in range(queue_count)]
set_queue(free_queue,queue_board,1)
return res
题52(困难):
分析:
解决了上一题,这题就是送啊
python代码:
python
class Solution:
def totalNQueens(self, n: int) -> int:
#n 皇后问题--》非线性--》选递归,
res=[]
queue_count=n
#设置皇后,递归函数,传当前可行位置+放置第几个
def set_queue(free_queue,queue_board,n):
if n<=0 or n>queue_count:
return
if n==queue_count:#放置第九个皇后
if 1 not in free_queue[n-1]:
return
else:
for i in range(queue_count):
if free_queue[n-1][i]==1:
queue_board[n-1][i]='Q'
res.append([''.join(i.copy()) for i in queue_board])
queue_board[n-1][i]='.'
return
else:
for i in range(queue_count):
if free_queue[n-1][i]==1:
free_queue_new = [i.copy() for i in free_queue]
queue_board_new = [i.copy() for i in queue_board]
queue_board_new[n-1][i]='Q'
for m in range(queue_count-n):
#设置纵列为0
free_queue_new[n+m][i]=0
#设置横列为0(要不要无所谓)
if i+m+1<queue_count:
free_queue_new[n-1][i+m+1]=0
#设置斜率为1的列为0
if n+m<queue_count and i+m+1<queue_count:
free_queue_new[n+m][i+m+1]=0
#设置斜率为-1的列为0
if n+m<queue_count and i-m-1>=0:
free_queue_new[n+m][i-m-1]=0
set_queue(free_queue_new,queue_board_new,n+1)
return
free_queue=[[1 for i in range(queue_count)] for j in range(queue_count)]
queue_board=[['.' for i in range(queue_count)] for j in range(queue_count)]
set_queue(free_queue,queue_board,1)
return len(res)
题53(中等):
分析:
python代码:
python
class Solution:
def maxSubArray(self, nums: List[int]) -> int:
#不用看也是动态规划啊,每一个现状等于之前状态加判断
res=0
n_len=len(nums)
n_list=[0 for i in range(n_len)]
for i in range(n_len):
n_list[i]=max(nums[i],n_list[i-1]+nums[i])
res=max(n_list)
return res
题54(中等):
分析:
会搞贪吃蛇小游戏的一个很清晰怎么搞
python代码:
python
class Solution:
def spiralOrder(self, matrix: List[List[int]]) -> List[int]:
row=len(matrix)
col=len(matrix[0])
cout=row*col
res=[]
#选则x,y作为当前位置
x,y=0,0
#和贪吃蛇一样选则p_x,p_y作为前进的方向
p_x,p_y=1,0
#定义终止转弯点
left,right,top,bottom=0,col-1,0,row-1
i=0
while i<cout:
res.append(matrix[y][x])
if x==right and y==top:
#到右上
if p_x==1:
top+=1
p_x,p_y=0,1
pass
if x==right and y==bottom:
#到右下
if p_y==1:
right-=1
p_x,p_y=-1,0
pass
if x==left and y==top:
#到左上
if p_y==-1:
left += 1
p_x,p_y=1,0
pass
if x==left and y==bottom:
#到左下
if p_x==-1:
bottom-=1
p_x,p_y=0,-1
pass
x+=p_x
y+=p_y
i+=1
return res
题55(中等):
分析:
之前没想到贪心,这次直接用贪心算法,(因为动态规划为n^2,时间复杂度高)
python代码:
python
class Solution:
def canJump(self, nums: List[int]) -> bool:
n_len=len(nums)
end,max_jump=0,0
for i in range(n_len):
if i>end:
return False
max_jump=max(max_jump,i+nums[i])
if max_jump>=n_len-1:
return True
if i==end:
end=max_jump