-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path59.SpiralMatrixIi.cpp
79 lines (75 loc) · 1.52 KB
/
59.SpiralMatrixIi.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
67
68
69
70
71
72
73
74
75
76
77
78
79
/*
* @lc app=leetcode id=59 lang=cpp
*
* [59] Spiral Matrix II
*
* https://leetcode.com/problems/spiral-matrix-ii/description/
*
* algorithms
* Medium (57.70%)
* Likes: 1587
* Dislikes: 127
* Total Accepted: 246.6K
* Total Submissions: 424.4K
* Testcase Example: '3'
*
* Given a positive integer n, generate an n x n matrix filled with elements
* from 1 to n^2 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
*
*
*/
// @lc code=start
#include <vector>
class Solution {
public:
std::vector<std::vector<int>> generateMatrix(int n) {
std::vector<std::vector<int>> result(n, std::vector<int>(n, 0));
int row = 0;
int step = 1;
while (row < n / 2) {
for (int i = row; i < n - row - 1; ++i) {
result[row][i] = step;
step++;
}
for (int i = row; i < n - row - 1; ++i) {
result[i][n - row - 1] = step;
step++;
}
for (int i = n - 1 - row; i > row; --i) {
result[n - row - 1][i] = step;
step++;
}
for (int i = n - 1 - row; i > row; --i) {
result[i][row] = step;
step++;
}
row++;
}
if (n % 2 == 1) {
result[row][row] = step;
}
return result;
}
};
// @lc code=end