-
Notifications
You must be signed in to change notification settings - Fork 522
/
Copy path07. Spiral Matrix II.cpp
66 lines (56 loc) · 1.18 KB
/
07. Spiral Matrix II.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
60
61
62
63
64
65
66
/*
Spiral Matrix II
================
Given a positive integer n, generate an n x n matrix filled with elements from 1 to n2 in spiral order.
Example 1:
Input: n = 3
Output: [[1,2,3],[8,9,4],[7,6,5]]
Example 2:
Input: n = 1
Output: [[1]]
Constraints:
1 <= n <= 20
*/
class Solution
{
public:
vector<vector<int>> generateMatrix(int n)
{
if (!n)
return {};
vector<vector<int>> matrix(n, vector<int>(n, 0));
int cnt = 1, tne = n * n, minr = 0, minc = 0, maxr = n - 1, maxc = n - 1;
while (cnt <= tne)
{
for (int j = minc; j <= maxc && cnt <= tne; j++)
{
matrix[minr][j] = cnt;
cout << cnt << " ";
cnt++;
}
minr++;
for (int i = minr; i <= maxr && cnt <= tne; i++)
{
matrix[i][maxc] = cnt;
cout << cnt << " ";
cnt++;
}
maxc--;
for (int j = maxc; j >= minc && cnt <= tne; j--)
{
matrix[maxr][j] = cnt;
cout << cnt << " ";
cnt++;
}
maxr--;
for (int i = maxr; i >= minr && cnt <= tne; i--)
{
matrix[i][minc] = cnt;
cout << cnt << " ";
cnt++;
}
minc++;
}
return matrix;
}
};