Skip to content

Latest commit

 

History

History
52 lines (41 loc) · 1.32 KB

0059-spiral-matrix-ii.adoc

File metadata and controls

52 lines (41 loc) · 1.32 KB

59. Spiral Matrix II

{leetcode}/problems/spiral-matrix-ii/[LeetCode - 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 ]
]

思路分析

利用 54. Spiral Matrix 的思路,从回溯思想得到启发,使用递归来逐层推进。每次方法调用只负责指定层的遍历,向里推进层次的工作,交给递归来完成。这样避免了复杂的判断。

{image_attr}
一刷
link:{sourcedir}/_0059_SpiralMatrixII.java[role=include]
二刷
link:{sourcedir}/_0059_SpiralMatrixII_2.java[role=include]