59.Spiral-Matrix-II
59. Spiral Matrix II
题目地址
https://leetcode.com/problems/spiral-matrix-ii/
题目描述
Given a positive integer n, generate a square matrix filled with elements from 1 to n2 in spiral order.
Example:
Input: 3
Output:
[
[ 1, 2, 3 ],
[ 8, 9, 4 ],
[ 7, 6, 5 ]
]代码
Approach 1: Recursive
1、生成二维数组
2、每次对n的外圈进行四次loop赋值
3、n-2进入下一次递归
Approach 2: Iterative
Approach #3 Traverse Layer by Layer in Spiral Form
Last updated
Was this helpful?