Problem
Problem page: https://leetcode.com/problems/spiral-matrix/
Topics: Array
Difficulty: Medium
Similar problems: (M) Spiral Matrix II
Given a matrix of
mxnelements (mrows,ncolumns), return all elements of the matrix in spiral order.For example,
Given the following matrix:
[ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ] ]
You should return[1,2,3,6,9,8,7,4,5].
Analysis
Another index calculation problem. I use two nested for loops to traverse the 2D array in spiral order.
Code
1 | class Solution: |
Article link: https://xnerv.wang/en/leetcode-spiral-matrix/
