algorithm-base/animation-simulation/数组篇/leetcode54螺旋矩阵.md

196 lines
5.8 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

> **[tan45du_one](https://raw.githubusercontent.com/tan45du/tan45du.github.io/master/个人微信.15egrcgqd94w.jpg)** ,备注 github + 题目 + 问题 向我反馈
>
>
>
> <u>[****](https://raw.githubusercontent.com/tan45du/test/master/微信图片_20210320152235.2pthdebvh1c0.png)</u> 两个平台同步,想要和题友一起刷题,互相监督的同学,可以在我的小屋点击<u>[**刷题小队**](https://raw.githubusercontent.com/tan45du/test/master/微信图片_20210320152235.2pthdebvh1c0.png)</u>进入。
#### [54. ](https://leetcode-cn.com/problems/spiral-matrix/)
* m* x nm , n
> matrix = [[1,2,3],[4,5,6],[7,8,9]]
> [1,2,3,6,9,8,7,4,5]
> matrix = [[1,2,3,4],[5,6,7,8],[9,10,11,12]]
> [1,2,3,4,8,12,11,10,9,5,6,7]
![](https://img-blog.csdnimg.cn/img_convert/cfa0192601dcc185e77125adc35e1cc5.png)*
![](https://img-blog.csdnimg.cn/20210318095839543.gif)
Java Code:
```java
class Solution {
public List<Integer> spiralOrder(int[][] matrix) {
List<Integer> arr = new ArrayList<>();
int left = 0, right = matrix[0].length-1;
int top = 0, down = matrix.length-1;
while (true) {
for (int i = left; i <= right; ++i) {
arr.add(matrix[top][i]);
}
top++;
if (top > down) break;
for (int i = top; i <= down; ++i) {
arr.add(matrix[i][right]);
}
right--;
if (left > right) break;
for (int i = right; i >= left; --i) {
arr.add(matrix[down][i]);
}
down--;
if (top > down) break;
for (int i = down; i >= top; --i) {
arr.add(matrix[i][left]);
}
left++;
if (left > right) break;
}
return arr;
}
}
```
C++ Code:
```cpp
class Solution {
public:
vector<int> spiralOrder(vector<vector<int>>& matrix) {
vector <int> arr;
int left = 0, right = matrix[0].size()-1;
int top = 0, down = matrix.size()-1;
while (true) {
for (int i = left; i <= right; ++i) {
arr.emplace_back(matrix[top][i]);
}
top++;
if (top > down) break;
for (int i = top; i <= down; ++i) {
arr.emplace_back(matrix[i][right]);
}
right--;
if (left > right) break;
for (int i = right; i >= left; --i) {
arr.emplace_back(matrix[down][i]);
}
down--;
if (top > down) break;
for (int i = down; i >= top; --i) {
arr.emplace_back(matrix[i][left]);
}
left++;
if (left > right) break;
}
return arr;
}
};
```
Python3 Code:
```python
from typing import List
class Solution:
def spiralOrder(self, matrix: List[List[int]])->List[int]:
arr = []
left = 0
right = len(matrix[0]) - 1
top = 0
down = len(matrix) - 1
while True:
for i in range(left, right + 1):
arr.append(matrix[top][i])
top += 1
if top > down:
break
for i in range(top, down + 1):
arr.append(matrix[i][right])
right -= 1
if left > right:
break
for i in range(right, left - 1, -1):
arr.append(matrix[down][i])
down -= 1
if top > down:
break
for i in range(down, top - 1, -1):
arr.append(matrix[i][left])
left += 1
if left > right:
break
return arr
```
Swift Code
```swift
class Solution {
func spiralOrder(_ matrix: [[Int]]) -> [Int] {
var arr:[Int] = []
var left = 0, right = matrix[0].count - 1
var top = 0, down = matrix.count - 1
while (true) {
for i in left...right {
arr.append(matrix[top][i])
}
top += 1
if top > down { break }
for i in top...down {
arr.append(matrix[i][right])
}
right -= 1
if left > right { break}
for i in stride(from: right, through: left, by: -1) {
arr.append(matrix[down][i])
}
down -= 1
if top > down { break}
for i in stride(from: down, through: top, by: -1) {
arr.append(matrix[i][left])
}
left += 1
if left > right { break}
}
return arr
}
}
```