54. 螺旋矩阵
题目
Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral order.
给个m * n 的矩阵,返回顺时针转一圈的数组。
思路
感觉有点像纯数学问题,每遍历n次,n - 1, 遍历m,每两次遍历切换正负遍历方向。
Example 1:
Input: [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ] ] Output: [1,2,3,6,9,8,7,4,5]
for i = 0; i < n * m; i++
m = 3; n = 3;
- row = 0, col = 0; i = 0;
- row = 0, col = 1; i = 1; col + 1
- row = 0, col = 2; i = 2; col + 1
- row = 1, col = 2; i = 3; row + 1
- row = 2, col = 2; i = 4; row + 1
- row = 2, col = 1; i = 5; col - 1
- row = 2, col = 0; i = 6; col - 1
- row = 1, col = 0; i = 7; row - 1
- row = 1, col = 1; i = 8; col - 1
Example 2:
Input:
[
[1, 2, 3, 4],
[5, 6, 7, 8],
[9,10,11,12]
]
Output: [1,2,3,4,8,12,11,10,9,5,6,7]
for i = 0; i < n * m; i++
m = 3; n = 4;
- row = 0, col = 0; i = 0;
- row = 0, col = 1; i = 1; col + 1
- row = 0, col = 2; i = 2; col + 1
- row = 0, col = 3, i = 3; col + 1
- row = 1, col = 3; i = 4; row + 1
- row = 2, col = 3; i = 5; row + 1
- row = 2, col = 2; i = 6; col - 1
- row = 2, col = 1; i = 7; col - 1
- row = 2, col = 0; i = 8; col - 1
- row = 1, col = 0; i = 9; row - 1
- row = 1, col = 1; i = 10; col + 1
- row = 1, col = 2; i = 11; col + 1
代码
|
|
赏析
无, 这段代码现在100%速度,想转成ts试下,发现leetcode ts无法解析=- =