mirror of
https://github.com/chefyuan/algorithm-base.git
synced 2026-03-12 21:01:08 +00:00
Merge branch 'main' into swift
# Conflicts: # animation-simulation/数组篇/leetcode219数组中重复元素2.md # animation-simulation/数组篇/leetcode59螺旋矩阵2.md # animation-simulation/数组篇/leetcode66加一.md # animation-simulation/数组篇/leetcode75颜色分类.md
This commit is contained in:
@@ -85,6 +85,42 @@ class Solution {
|
||||
|
||||
```
|
||||
|
||||
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
|
||||
|
||||
Reference in New Issue
Block a user