-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathPrintMatrix.java
66 lines (51 loc) · 1.45 KB
/
PrintMatrix.java
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
package org.offer.case20;
/**
* 面试题 20:顺时针打印矩阵
* Created by tanc on 2017/4/6.
*/
public class PrintMatrix {
public static <T> void printInCircle(T[][] arr) {
if (null == arr) {
return;
}
int rows = arr.length;
int cols = arr[0].length;
int maxX = cols - 1;
int maxY = rows - 1;
int start = 0;
while (cols > start * 2 && rows > start * 2) {
printInCircle(arr, maxX, maxY, start);
start++;
}
}
private static <T> void printInCircle(T[][] arr, int x, int y, int start) {
int endX = x - start;
int endY = y - start;
// 从左到右打印
for (int i = start; i <= endX; i++ ) {
printItem(arr[start][i]);
}
// 从上到下
if (start < endY) {
for (int i = start + 1; i <= endY; i++) {
printItem(arr[i][endX]);
}
}
// 从右到左
if (start < endX && start < endY) {
for (int i = endX - 1; i >= start; i--) {
printItem(arr[endY][i]);
}
}
// 从下到上
if (start + 1 < endY && start < endX) {
for (int i = endY - 1; i > start; i--) {
printItem(arr[i][start]);
}
}
}
private static <T> void printItem(T item) {
System.out.print(item);
System.out.print(",");
}
}