-
Notifications
You must be signed in to change notification settings - Fork 0
/
shunshizhendayin.cpp
78 lines (66 loc) · 2.05 KB
/
shunshizhendayin.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
#include <vector>
using namespace std;
class Solution {
public:
vector<int> printMatrix(vector<vector<int> > mat) {
const int n=mat.size();
if(n==0){
return vector<int>();
}else{
const int m=mat[0].size();
const int eleSize=n*m;
std::vector<int> res(eleSize,0);
int startX=0;
int endX=n-1;
int startY=0;
int endY=m-1;
/*
* matrix
* 0,0 0,1 ... 0,m-1
* 1,0
* .
* .
* .
* n-1,0 n-1,1 ... n-1,m-1
*/
int i=0;
int j=0;
int k=0;
while(startX<=endX&&startY<=endY){
//当前仅有一行
if(startX==endX){
for(;j<=endY;++j)
res[k++]=mat[startX][j];
return res;
}
//当前仅有一列
if(startY==endY){
for(;i<=endX;++i)
res[k++]=mat[i][startY];
return res;
}
//顺时针遍历矩阵
//1、打印最上面的行,除去最右边的元素
for(;j<endY;++j)
res[k++]=mat[i][j];
//2、打印右边的行,除去最下面的一个元素
for(;i<endX;++i)
res[k++]=mat[i][j];
//3、打印下面的行,除去最左边的元素
for(;j>startY;--j)
res[k++]=mat[i][j];
//4、打印最左边的行,除去最上面的那个元素
for(;i>startX;--i)
res[k++]=mat[i][j];
//缩小范围
i++;
j++;
startX++;
endX--;
startY++;
endY--;
}//end of while
return res;
}
}
};