-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSpiralMatrix.java
More file actions
133 lines (120 loc) · 2.69 KB
/
SpiralMatrix.java
File metadata and controls
133 lines (120 loc) · 2.69 KB
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package codingInterview;
import java.util.ArrayList;
public class SpiralMatrix {
public static void main(String[] args) {
// TODO Auto-generated method stub
int[][] mat = { { 1, 2, 3 },
{ 4, 5, 6 },
{ 7, 8, 9 },
{ 10, 11, 12 } };
ArrayList<Integer> result = spiralOrder(mat);
printMat(result);
result = spiralRecursively(mat);
printMat(result);
}
/**
* recursively
*
* @param mat
* @return
*/
private static ArrayList<Integer> spiralRecursively(int[][] mat) {
// TODO Auto-generated method stub
if (mat == null) {
return null;
}
return spiralRecursively(mat, 0, 0, mat.length, mat[0].length);
}
private static ArrayList<Integer> spiralRecursively(int[][] mat, int x,
int y, int m, int n) {
// TODO Auto-generated method stub
ArrayList<Integer> result = new ArrayList<Integer>();
if (m <= 0 || n <= 0) {
return result;
}
if (m == 1) {
for (int i = 0; i < n; i++) {
result.add(mat[x][y++]);
}
return result;
} else if (n == 1) {
for (int i = 0; i < m; i++) {
result.add(mat[x++][y]);
}
return result;
}
for (int i = 0; i < n - 1; i++) {
result.add(mat[x][y++]);
}
for (int i = 0; i < m - 1; i++) {
result.add(mat[x++][y]);
}
for (int i = 0; i < n - 1; i++) {
result.add(mat[x][y--]);
}
for (int i = 0; i < m - 1; i++) {
result.add(mat[x--][y]);
}
if (m == 1 || n == 1) {
result.addAll(spiralRecursively(mat, x + 1, y + 1, m, n));
} else {
result.addAll(spiralRecursively(mat, x + 1, y + 1, m - 2, n - 2));
}
return result;
}
private static void printMat(ArrayList<Integer> result) {
// TODO Auto-generated method stub
if (result == null) {
System.out.println("null");
}
for (int i : result) {
System.out.print(i + " ");
}
System.out.println();
}
/**
* handle the matrix like a circle
*
* @param mat
* @return
*/
private static ArrayList<Integer> spiralOrder(int[][] mat) {
// TODO Auto-generated method stub
ArrayList<Integer> result = new ArrayList<Integer>();
if (mat == null) {
return null;
}
int m = mat.length, n = mat[0].length;
int x = 0, y = 0;
while (m > 0 && n > 0) {
if (m == 1) {
for (int i = 0; i < n; i++) {
result.add(mat[x][y++]);
}
break;
} else if (n == 1) {
for (int i = 0; i < m; i++) {
result.add(mat[x++][y]);
}
break;
}
for (int i = 0; i < n - 1; i++) {
result.add(mat[x][y++]);
}
for (int i = 0; i < m - 1; i++) {
result.add(mat[x++][y]);
}
for (int i = 0; i < n - 1; i++) {
result.add(mat[x][y--]);
}
for (int i = 0; i < m - 1; i++) {
result.add(mat[x--][y]);
}
x++;
y++;
m = m - 2;
n = n - 2;
}
return result;
}
}