-
Notifications
You must be signed in to change notification settings - Fork 0
/
spiral_matrix2.cpp
59 lines (54 loc) · 1.38 KB
/
spiral_matrix2.cpp
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
// 59. Spiral Matrix II: https://leetcode.com/problems/spiral-matrix-ii
// Author: [email protected]
#include <stdio.h>
#include <vector>
class Solution
{
public:
std::vector<std::vector<int>> generateMatrix(int n)
{
std::vector<std::vector<int>> matrix(n, std::vector<int>(n, 0));
std::vector<int> dr = {0, 1, 0, -1}; // right, down, left, up
std::vector<int> dc = {1, 0, -1, 0}; // right, down, left, up
int di = 0; // direction index
int ri = 0; // row index
int ci = 0; // column index
int val = 1;
for (int i = 0; i < n * n; i++)
{
matrix[ri][ci] = val++;
int nr = ri + dr[di]; // next row
int nc = ci + dc[di]; // next column
if (nr >= 0 && nr < n && nc >= 0 && nc < n && !matrix[nr][nc])
{
ri = nr;
ci = nc;
}
else
{
di = (di + 1) % 4;
ri += dr[di];
ci += dc[di];
}
}
return matrix;
}
};
int main(int argc, char* argv[])
{
int n = 3;
if (argc > 1)
{
n = atoi(argv[1]);
}
auto matrix = Solution().generateMatrix(n);
for (const auto& row: matrix)
{
for (auto val: row)
{
printf("%d ", val);
}
printf("\n");
}
return 0;
}