[LeetCode] Spiral Matrix

54. Spiral Matrix

Given an m x n matrix, return all elements of the matrix in spiral order.

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
26
27
class Solution {
const int visit = -101;
int dx[4] = {0, 1, 0, -1};
int dy[4] = {-1, 0, 1, 0};

int getNextDir(int y, int x, int n, int m, vector<vector<int>>& matrix, int dir) {
int ny = y + dy[dir];
int nx = x + dx[dir];
if(0 <= nx && nx < m && 0 <= ny && ny < n && matrix[ny][nx] != visit) return dir;
return (dir + 1) % 4;
}
public:
vector<int> spiralOrder(vector<vector<int>>& matrix) {
int n = matrix.size(), m = matrix[0].size();
int y = 0, x = 0, idx = 0, dir = 1;
vector<int> res(n * m);
while(idx < n * m) {
res[idx++] = matrix[y][x];
matrix[y][x] = visit;
dir = getNextDir(y, x, n, m, matrix, dir);
y += dy[dir];
x += dx[dir];
}

return res;
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2021/04/24/PS/LeetCode/spiral-matrix/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.