forked from luliyucoordinate/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
0054.cpp
35 lines (33 loc) · 1021 Bytes
/
0054.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
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
static int x = [](){std::ios::sync_with_stdio(false);cin.tie(0);return 0;}();
class Solution
{
public:
vector<int> spiralOrder(vector<vector<int>>& matrix)
{
vector<int> result;
if (matrix.empty())
return result;
int x1 = 0, y1 = 0, x2 = matrix[0].size()-1, y2 = matrix.size()-1;
while (x1 <= x2 and y1 <= y2)
{
for (int c = x1; c <= x2; c++) result.push_back(matrix[y1][c]);
for (int r = y1 + 1; r <= y2; r++) result.push_back(matrix[r][x2]);
if (x1 < x2 and y1 < y2)
{
for (int c = x2 - 1; c > x1; c--) result.push_back(matrix[y2][c]);
for (int r = y2; r > y1; r--) result.push_back(matrix[r][x1]);
}
x1++;x2--;y1++;y2--;
}
return result;
}
};
int main()
{
vector<vector<int>> nums = {{1,2,3}};
auto result = Solution().spiralOrder(nums);
}