54.Spiral-Matrix
54. Spiral Matrix
题目地址
https://leetcode.com/problems/spiral-matrix/
题目描述
Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral order.
Example 1:
Input:
[
[ 1, 2, 3 ],
[ 4, 5, 6 ],
[ 7, 8, 9 ]
]
Output: [1,2,3,6,9,8,7,4,5]
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]代码
Approach 1: Simulation
Complexity Analysis
Time Complexity: O(_N), where N_ is the total number of elements in the input matrix. We add every element in the matrix to our final answer.
Space Complexity: O_(_N), the information stored in
seenand inans.
Approach 2: Layer-by-Layer
Last updated
Was this helpful?