题目链接 : https://leetcode-cn.com/problems/spiral-matrix/
题目描述:
给定一个包含 m x n 个元素的矩阵(m 行, n 列),请按照顺时针螺旋顺序,返回矩阵中的所有元素。
示例:
输入:
[
[ 1, 2, 3 ],
[ 4, 5, 6 ],
[ 7, 8, 9 ]
]
输出: [1,2,3,6,9,8,7,4,5]
示例 2:
输入:
[
[1, 2, 3, 4],
[5, 6, 7, 8],
[9,10,11,12]
]
输出: [1,2,3,4,8,12,11,10,9,5,6,7]
思路:
思路一:模拟过程
通过控制行的上下边界,列的左右边界
思路二: 旋转
直接举例子,
1 2 3 弹出第一行,逆时针旋转 6 9 重复操作 8 7
4 5 6 ------------------> 5 8 ----> 5 4 --->...
7 8 9 4 7
把弹出输出即可.
关注我的知乎专栏,了解更多解题技巧,大家共同进步!
代码:
思路一:
python
class Solution:
def spiralOrder(self, matrix):
"""
:type matrix: List[List[int]]
:rtype: List[int]
"""
if not matrix : return []
shang_row = 0
xia_row = len(matrix) - 1
zuo_col = 0
you_col = len(matrix[0]) - 1
res = []
while shang_row <= xia_row and zuo_col <= you_col:
# 从左到右
for i in range(zuo_col, you_col+1):
#print(shang_row, i)
res.append(matrix[shang_row][i])
shang_row += 1
if shang_row > xia_row:break
# 从上到下
for i in range(shang_row, xia_row+1):
res.append(matrix[i][you_col])
you_col -= 1
if zuo_col > you_col:break
# 从右到左
for i in range(you_col, zuo_col - 1,-1):
#print(xia_row - 1, i)
res.append(matrix[xia_row][i])
xia_row -= 1
# 从下到上
for i in range(xia_row , shang_row - 1, -1):
#print(i, zuo_col)
res.append(matrix[i][zuo_col])
zuo_col += 1
return res
java
class Solution {
public List<Integer> spiralOrder(int[][] matrix) {
if (matrix == null || matrix.length == 0) return new ArrayList<>();
List<Integer> res = new ArrayList<>();
int shang_row = 0;
int xia_row = matrix.length - 1;
int zou_col = 0;
int you_col = matrix[0].length - 1;
while (shang_row <= xia_row && zou_col <= you_col) {
// 从左到右
for (int i = zou_col; i <= you_col; i++) res.add(matrix[shang_row][i]);
shang_row++;
if (shang_row > xia_row) break;
// 从上到下
for (int i = shang_row; i <= xia_row; i++) res.add(matrix[i][you_col]);
you_col--;
if (zou_col > you_col) break;
// 从右到左
for (int i = you_col; i >= zou_col; i--) res.add(matrix[xia_row][i]);
xia_row--;
//从下到上
for (int i = xia_row; i >= shang_row; i--) res.add(matrix[i][zou_col]);
zou_col++;
}
return res;
}
}
思路二:
class Solution:
def spiralOrder(self, matrix: List[List[int]]) -> List[int]:
if not matrix : return []
res = []
while matrix:
res.extend(matrix.pop(0))
next_matrix = []
#print(matrix)
for x in zip(*matrix):
next_matrix.append(x)
#print(next_matrix)
matrix = next_matrix[::-1]
return res