Description
Given a positive integer n, generate a square matrix filled with elements from 1 to n2 in spiral order.
Example:
1 | Input: 3 |
解法
之前的那道题是螺旋形遍历数组,这题是螺旋形生成数组,方法都差不多,设置边界与方向,为二维数组赋值,同样注意处理1和0输入的情况。感觉第二次写这类题思路顺畅了很多呢(^_^)~
具体代码如下:
1 | class Solution { |