Files
2020-08-07 17:06:53 +08:00

41 lines
1003 B
Markdown
Executable File
Raw Permalink Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# [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]
## 题目大意
给定一个包含 m x n 个元素的矩阵m 行, n 列),请按照顺时针螺旋顺序,返回矩阵中的所有元素。
## 解题思路
- 给出一个二维数组,按照螺旋的方式输出
- 解法一:需要注意的是特殊情况,比如二维数组退化成一维或者一列或者一个元素。注意了这些情况,基本就可以一次通过了。
- 解法二提前算出一共多少个元素一圈一圈地遍历矩阵停止条件就是遍历了所有元素count == sum