-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path029-顺时针打印矩阵.cpp
34 lines (33 loc) · 1.14 KB
/
029-顺时针打印矩阵.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
class Solution {
public:
vector<int> printMatrix(vector<vector<int> > matrix) {
vector<int> res;
if(matrix.empty()||matrix[0].empty())
return res;
int rowLength=matrix.size(), colLength=matrix[0].size();
int startRow=0, startCol=0;
int endRow=rowLength-startRow-1,endCol=colLength-startCol-1;
while(rowLength>startRow*2&&colLength>startCol*2){
for(int i=startCol;i<=endCol;i++){
res.push_back(matrix[startRow][i]);
}
if(endRow>startRow){
for(int i=startRow+1;i<=endRow;i++)
res.push_back(matrix[i][endCol]);
}
if(endRow>startRow&&endCol>startCol){
for(int i=endCol-1;i>=startCol;i--)
res.push_back(matrix[endRow][i]);
}
if(endRow-startRow>=2&&endCol>startCol){
for(int i=endRow-1;i>=startRow+1;i--)
res.push_back(matrix[i][startCol]);
}
startRow++;
startCol++;
endRow--;
endCol--;
}
return res;
}
};