## 题目地址

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]
``````

1　2　3

4　5　6

7　8　9

## 代码

``````class Solution {
public:
vector<int> spiralOrder(vector<vector<int> > &matrix) {
if (matrix.empty() || matrix[0].empty()) return {};
int m = matrix.size(), n = matrix[0].size();
vector<int> res;
int c = m > n ? (n + 1) / 2 : (m + 1) / 2;
int p = m, q = n;
for (int i = 0; i < c; ++i, p -= 2, q -= 2) {
for (int col = i; col < i + q; ++col)
res.push_back(matrix[i][col]);
for (int row = i + 1; row < i + p; ++row)
res.push_back(matrix[row][i + q - 1]);
if (p == 1 || q == 1) break;
for (int col = i + q - 2; col >= i; --col)
res.push_back(matrix[i + p - 1][col]);
for (int row = i + p - 2; row > i; --row)
res.push_back(matrix[row][i]);
}
return res;
}
};

``````

``````class Solution {
public:
vector<int> spiralOrder(vector<vector<int>>& matrix) {
if (matrix.empty() || matrix[0].empty()) return {};
int m = matrix.size(), n = matrix[0].size();
vector<int> res;
int up = 0, down = m - 1, left = 0, right = n - 1;
while (true) {
for (int j = left; j <= right; ++j) res.push_back(matrix[up][j]);
if (++up > down) break;
for (int i = up; i <= down; ++i) res.push_back(matrix[i][right]);
if (--right < left) break;
for (int j = right; j >= left; --j) res.push_back(matrix[down][j]);
if (--down < up) break;
for (int i = down; i >= up; --i) res.push_back(matrix[i][left]);
if (++left > right) break;
}
return res;
}
};

``````

``````class Solution {
public:
vector<int> spiralOrder(vector<vector<int>>& matrix) {
if (matrix.empty() || matrix[0].empty()) return {};
int m = matrix.size(), n = matrix[0].size(), idx = 0, i = 0, j = 0;
vector<int> res;
vector<vector<int>> dirs{{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
for (int k = 0; k < m * n; ++k) {
res.push_back(matrix[i][j]);
matrix[i][j] = 0;
int x = i + dirs[idx][0], y = j + dirs[idx][1];
if (x < 0 || x >= m || y < 0 || y >= n || matrix[x][y] == 0) {
idx = (idx + 1) % 4;
x = i + dirs[idx][0];
y = j + dirs[idx][1];
}
i = x;
j = y;
}
return res;
}
};
``````

评论
0 评论