forked from shijiebei2009/Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPrintArray.java
92 lines (84 loc) · 2.71 KB
/
PrintArray.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package cn.codepub.algorithms.arrays;
import java.util.ArrayList;
import java.util.List;
/**
* <p>
* Created with IntelliJ IDEA. 2015/10/29 21:29
* </p>
* <p>
* ClassName:PrintArray
* </p>
* <p>
* Description:顺时针打印二位数组
* </P>
*
* @author Wang Xu
* @version V1.0.0
* @since V1.0.0
*/
public class PrintArray {
static int nums[][];
static List<Integer> list = new ArrayList<>();
public static void main(String[] args) {
nums = new int[][]{{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}};
moveRight(0, 0, nums.length, nums[0].length, 0, nums.length * nums[0].length);
System.out.println(list);
list.clear();
nums = new int[][]{{1}, {2}};
moveRight(0, 0, nums.length, nums[0].length, 0, nums.length * nums[0].length);
System.out.println(list);
list.clear();
}
private static void moveRight(int curRow, int curCol, int rows, int cols, int temp, int size) {
if (size <= 0) {
return;
}
if (curCol + 1 < cols - temp) {
list.add(nums[curRow][curCol]);
curCol++;
size--;
moveRight(curRow, curCol, rows, cols, temp, size);
} else {
moveDown(curRow, curCol--, rows, cols, temp, size);
}
}
private static void moveDown(int curRow, int curCol, int rows, int cols, int temp, int size) {
if (size <= 0) {
return;
}
if (curRow + 1 < rows - temp) {
list.add(nums[curRow][curCol]);
size--;
curRow++;
moveDown(curRow, curCol, rows, cols, temp, size);
} else {
moveLeft(curRow--, curCol, rows, cols, temp, size);
}
}
private static void moveLeft(int curRow, int curCol, int rows, int cols, int temp, int size) {
if (size <= 0) {
return;
}
if (curCol - 1 >= temp - 1) {
list.add(nums[curRow][curCol]);
size--;
curCol--;
moveLeft(curRow, curCol, rows, cols, temp, size);
} else {
moveUp(curRow, ++curCol, rows, cols, temp + 1, size);
}
}
private static void moveUp(int curRow, int curCol, int rows, int cols, int temp, int size) {
if (size <= 0) {
return;
}
if (curRow - 1 > temp - 1) {
curRow--;
list.add(nums[curRow][curCol]);
size--;
moveUp(curRow, curCol, rows, cols, temp, size);
} else {
moveRight(curRow, ++curCol, rows, cols, temp, size);
}
}
}