给你一个正整数 n ,生成一个包含 1 到 n2 所有元素,且元素按顺时针顺序螺旋排列的 n x n 正方形矩阵 matrix 。
示例 1:
输入:n = 3
输出:[[1,2,3],[8,9,4],[7,6,5]]
示例 2:
输入:n = 1
输出:[[1]]
提示:
1 <= n <= 20
# @lc app=leetcode.cn id=59 lang=python3
#
# [59] 螺旋矩阵 II
#
# @lc code=start
from calendar import c
class Solution:
def generateMatrix(self, n: int) -> List[List[int]]:
rev=[[0]*n for i in range(n)]
rstart=0
cstart=0
rend=n-1
cend=n-1
mid=1
while cend>=cstart and rstart<=rend:
for l in range(cstart,cend+1):
rev[rstart][l]=mid
mid=mid+1
#rev.extend(matrix[rstart][cstart:cend+1])
for l in range(rstart+1,rend):
rev[l][cend]=mid
mid=mid+1
for l in range(cend,cstart-1,-1):
rev[rend][l]=mid
mid=mid+1
#rev.extend(matrix[rend][cstart:cend+1][::-1])
for l in range(rend-1,rstart,-1):
rev[l][cstart]=mid
mid=mid+1
cend=cend-1
cstart=cstart+1
rstart=rstart+1
rend=rend-1
if n//2!=n/2:
rev[n//2][n//2]=n*n
return rev
# @lc app=leetcode.cn id=55 lang=python3
#
# [55] 跳跃游戏
#
# @lc code=start
class Solution:
def canJump(self, nums: List[int]) -> bool:
re=0
if nums==[0]:
return True
if 0 not in nums:
return True
for i in range(len(nums)-1,-1,-1):
if nums[i]==0:
j=0
re=0
while j<i:
if nums[j]<=(i-j) and i!=len(nums)-1:
j=j+1
continue
elif nums[j]<(i-j) and i==len(nums)-1:
j=j+1
continue
else:
re=1 # True
break
if re==0:
return False
if re == 1:
return True
else:return False
#return False
# @lc app=leetcode.cn id=54 lang=python3
#
# [54] 螺旋矩阵
#
# @lc code=start
class Solution:
def spiralOrder(self, matrix: List[List[int]]) -> List[int]:
rstart=0
cstart=0
rend=len(matrix)-1
cend=len(matrix[0])-1
rev=list()
if cstart==cend:
for i in matrix:
rev.extend(i)
return rev
if rstart==rend:
return matrix[0]
while rstart<rend and cstart<cend :
print(rstart,rend,cstart,cend)
for l in range(cstart,cend+1):
rev.append(matrix[rstart][l])
#rev.extend(matrix[rstart][cstart:cend+1])
for l in range(rstart+1,rend):
rev.append(matrix[l][cend])
for l in range(cend,cstart-1,-1):
rev.append(matrix[rend][l])
#rev.extend(matrix[rend][cstart:cend+1][::-1])
for l in range(rend-1,rstart,-1):
rev.append(matrix[l][cstart])
#print(rev)
cend=cend-1
cstart=cstart+1
rstart=rstart+1
rend=rend-1
if rstart==rend and cstart!=cend:
print("fff")
for l in range(cstart,cend+1):
#print(matrix[rstart][l])
rev.append(matrix[rstart][l])
elif cstart==cend and rstart!=rend:
for l in range(rstart,rend+1):
#print(matrix[l][cstart])
rev.append(matrix[l][cstart])
elif rstart==rend and cstart==cend:
rev.append(matrix[rstart][cstart])
return rev
# @lc code=end
# @lc app=leetcode.cn id=49 lang=python3
#
# [49] 字母异位词分组
#
# @lc code=start
class Solution:
def groupAnagrams(self, strs: List[str]) -> List[List[str]]:
def matchstr(s,p):
lens=len(s)
lenp=len(p)
if lens==lenp:
for i in range(len(p)):
if p[i] in s:
h=s.index(p[i])
s=s[0:h]+s[h+1:]
continue
else:
return False
return True
else:return False
rev=list()
rev.append([strs[0]])
for i in range(1,len(strs)):
k=0
for j in range(len(rev)):
#print(rev,j,i)
if matchstr(rev[j][0],strs[i]):
rev[j].append(strs[i])
k=1
break
if k==0:
rev.append([strs[i]])
return rev
# @lc code=start
class Solution:
def rotate(self, matrix: List[List[int]]) -> None:
"""
Do not return anything, modify matrix in-place instead.
"""
mid=0
n=len(matrix)
for i in range(n):
for j in range(i):
mid = matrix[i][j]
matrix[i][j] = matrix[j][i]
matrix[j][i] = mid
for i in range(n):
for j in range(n//2):
mid=matrix[i][j]
matrix[i][j]=matrix[i][n-j-1]
matrix[i][n-j-1] = mid
# @lc code=end
# [46] 全排列
#
# @lc code=start
class Solution:
def permute(self, nums: List[int]) -> List[List[int]]:
import copy
def subpermute(nums,index):
if index==0:
return [[nums[0]]]
else:
rev=subpermute(nums,index-1)
res=list()
for i in range(len(rev)):
#print(rev[i])
for j in range(len(rev[i])+1):
if j==len(rev[i]) or rev[i][j]!=nums[index]:
mid=copy.deepcopy(rev[i])
#print(mid)
mid.insert(j,nums[index])
if mid not in res:
res.append(mid)
return res
index=len(nums)-1
return subpermute(nums,index)