-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path54.cpp
More file actions
executable file
·25 lines (25 loc) · 823 Bytes
/
54.cpp
File metadata and controls
executable file
·25 lines (25 loc) · 823 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <string>
#include <vector>
using namespace std;
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;
}
};