-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoffer_29
52 lines (43 loc) · 1.04 KB
/
offer_29
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
//面试题29:顺时针打印矩阵
void PrintMatrixClockwisely(int** numbers,int columns,int rows)
{
if(number==nullptr || colunms<=0 || rows<=0)
return;
int start=0;
while(columns>start*2 && rows>start*2)
{
PrintMatrixInCircle(numbers,columns,rows,start);
++start;
}
}
void PrintMatrixInCircle(int** numbers,int columns,int rows,int start)
{
int endX=columns-1-start;
int endY=rows-1-start;
//从左到右打印一行
for(int i=start;i<=endX;++i){
int number=number[start][i];
printNumber(number);
}
//从上到下打印一行
if(start<endY){
for(int i=start+1;i<endY;++i){
int number=number[i][endX];
printNumber[number];
}
}
//从右到左打印一行
if(start<endX && start<endY){
for(int i=endX-1;i>=start;--i){
int number=number[endY][i];
printNumber(number);
}
}
//从下到上打印一行
if(start<endX && start<endY-1){
for(int i=endY-1;i>=start+1;--i){
int number=number[i][start];
printNumber(number);
}
}
}