偏移量方法:使用偏移量解,碰壁转向
class Solution(object):
def spiralOrder(self, matrix):
"""
:type matrix: List[List[int]]
:rtype: List[int]
"""
if not matrix:
return []
if matrix == []:
return matrix
rows = len(matrix)
columns = len(matrix[0])
# 偏移量
dx = (0, 1, 0, -1) # row index's change in 4 directions,[右、下、左、上]
dy = (1, 0, -1, 0) # column index's change in 4 directions,[右、下、左、上]
res = [] # 结果一维维列表
visited = [[-1 for _ in range(columns)] for _ in range(rows)] # 标记矩阵,访问过就置为1
# flattenMatrix = [n for row in matrix for n in row] # Matrix二维转一维
x = 0
y = 0
d = 0
for i in range(rows * columns): # 一共就rows*columns个数
res.append(matrix[x][y])
visited[x][y] = 1
# 移动
nx = x + dx[d]
ny = y + dy[d]
# 如果超出边界,或者格子已经被访问
if nx < 0 or nx >= rows or ny < 0 or ny >= columns or visited[nx][ny] != -1:
d = (d + 1) % 4
nx = x + dx[d]
ny = y + dy[d]
# 更新坐标
x = nx
y = ny
return res
matrix = [[1, 2, 3, 4],
[5, 6, 7, 8],
[9, 10, 11, 12],
[13, 14, 15, 16]]
matrix2 = [[1], [2], [3], [4], [5]]
matrix3 = [[1, 2], [3, 4], [5, 6], [7, 8], [9, 10]]
s = Solution()
print(s.spiralOrder(matrix))
print(s.spiralOrder(matrix3))
https://blog.csdn.net/zhushaojiecumt/article/details/102920530
其他解法:
剑指offer 29:顺时针打印矩阵 https://blog.csdn.net/IOT_victor/article/details/90676900
class Solution(object):
def generateMatrix(self, n):
"""
:type n: int
:rtype: List[List[int]]
"""
# 通用:n行,m列
if n == 0:
return
m = n # 列数
# 偏移量
dx = (0, 1, 0, -1) # row index's change in 4 directions,[右、下、左、上]
dy = (1, 0, -1, 0) # column index's change in 4 directions,[右、下、左、上]
# res = [] # 结果矩阵:二维列表
# for i in range(n):
# res.append(list())
# for j in range(n):
# res[i].append(-1)
res = [[-1 for _ in range(m)] for _ in range(n)] # 不能写'-1'
x = 0
y = 0
d = 0
for k in range(1, n*m+1): # # k 从 1 到 n*n
res[x][y] = k
# 移动
nx = x + dx[d]
ny = y + dy[d]
# 如果超出边界,或者格子已经被访问
if nx < 0 or nx >= n or ny < 0 or ny >= m or res[nx][ny] != -1:
d = (d + 1) % 4
nx = x + dx[d]
ny = y + dy[d]
# 更新坐标
x = nx
y = ny
return res
n = 3
s = Solution()
print(s.generateMatrix(n))
https://leetcode-cn.com/problems/spiral-matrix-ii/solution/gu-ding-tao-lu-peng-dao-bian-jie-jiu-zhuan-xiang-p/
奇数n*n从里向外,如图顺序打印
5*5的打印结果
[13, 8, 9, 14, 19, 18, 17, 12, 7, 2, 3, 4, 5, 10, 15, 20, 25, 24, 23, 22, 21, 16, 11, 6, 1]
思路:划分四个区间,建立第四象限直角坐标系
def func(matrix):
n = len(matrix)
x = n // 2 # 直角坐标系x坐标
y = -(n // 2) # 直角坐标系y坐标
res = []
res.append(matrix[-y][x]) # 注意:二维矩阵的索引,先row在column
# 先向上走一步,还有n*n-2个数待遍历
y += 1
res.append(matrix[-y][x])
# 直角坐标系第四象限
for i in range(n*n-2):
# 上三角形
if y > x-n+1 and y >= -x:
if y == -x:
y += 1 # 向上
res.append(matrix[-y][x])
else:
x += 1 # 向右
res.append(matrix[-y][x])
# 右三角形
elif y <= x-n+1 and y > -x:
y -= 1 # 向下
res.append(matrix[-y][x])
# 下三角形
elif y < x-n+1 and y <= -x:
x -= 1 # 向左
res.append(matrix[-y][x])
# 左三角形
elif y >= x-n+1 and y < -x:
y += 1 # 向上
res.append(matrix[-y][x])
return res
matrix = [[1,2,3,4,5],
[6,7,8,9,10],
[11,12,13,14,15],
[16,17,18,19,20],
[21,22,23,24,25]]
# matrix = [[1, 2, 3, 4, 5, 6, 7],
# [8, 9, 10,11,12,13,14],
# [15,16,17,18,19,20,21],
# [22,23,24,25,26,27,28],
# [29,30,31,32,33,34,35],
# [36,37,38,39,40,41,42],
# [43,44,45,46,47,48,49]]
ans = func(matrix)
print(ans)