-
Notifications
You must be signed in to change notification settings - Fork 0
/
leetcode54.cpp
38 lines (38 loc) · 1.06 KB
/
leetcode54.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
//螺旋输出矩阵的数字
class Solution {
public:
vector<int> spiralOrder(vector<vector<int>>& matrix) {
if(matrix.empty() || matrix[0].empty())
return vector<int>();
vector<int> path;
int top = 0;
int bot = matrix.size() - 1;
int lef = 0;
int rgt = matrix[0].size() - 1;
int i=0,j=0;
while(top <= bot && lef <=rgt)
{
while(top <= bot && lef <= rgt && j <= rgt)
path.push_back(matrix[i][j++]);
i = i + 1;
j = j - 1;
top = top + 1;
while(top <= bot && lef <= rgt && i <= bot)
path.push_back(matrix[i++][j]);
i = i - 1;
j = j - 1;
rgt = rgt - 1;
while(top <= bot && lef <= rgt && j >= lef)
path.push_back(matrix[i][j--]);
i = i - 1;
j = j + 1;
bot = bot - 1;
while(top <= bot && lef <= rgt && i >= top)
path.push_back(matrix[i--][j]);
i = i + 1;
j = j + 1;
lef = lef + 1;
}
return path;
}
};