-
Notifications
You must be signed in to change notification settings - Fork 1
/
spiralMatrix.java
98 lines (79 loc) · 2.34 KB
/
spiralMatrix.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
93
94
95
96
97
98
// leetcode Spiral Matrix
import java.util.ArrayList;
public class spiralMatrix {
public static void main(String args[]) {
Solution cl = new Solution();
int arr[][] = {{2},{3}};
ArrayList<Integer> list = cl.spiralOrder(arr);
for (int i : list)
System.out.print(" " + i);
System.out.println();
}
}
class Solution {
int mat[][];
public ArrayList<Integer> spiralOrder(int[][] matrix) {
// Start typing your Java solution below
// DO NOT write main() function
ArrayList<Integer> retlist = new ArrayList<Integer>();
if (null == matrix)
return retlist;
if (0 == matrix.length || 0 == matrix[0].length)
return retlist;
this.mat = matrix;
boolean arr[][] = new boolean[matrix.length][matrix[0].length];
int x = 0;
int y = 0;
int w = matrix[0].length - 1;
int h = matrix.length - 1;
recur(x, y, h, w, arr, retlist);
return retlist;
}
private void recur(int x, int y, int h, int w, boolean arr[][], ArrayList<Integer> list) {
//arr[x][y] = true;
// go right
// System.out.println(arr.length + " " + arr[0].length + " " + h + " " + w + " " + x + " " + y);
while (y <= w && true != arr[x][y]) {
arr[x][y] = true;
list.add(mat[x][y]);
y++;
}
y--;
if (x + 1 > h || false != arr[x + 1][y])
return;
x++;
// go down
while (x <= h && true != arr[x][y]) {
arr[x][y] = true;
list.add(mat[x][y]);
x++;
}
x--;
if (y - 1 < 0 || false != arr[x][y - 1])
return;
y--;
// go left
while (y >= 0 && true != arr[x][y]) {
arr[x][y] = true;
list.add(mat[x][y]);
y--;
}
y++;
if (x - 1 < 0 || false != arr[x - 1][y])
return;
x--;
// go up
while (x >= 0 && true != arr[x][y]) {
arr[x][y] = true;
list.add(mat[x][y]);
x--;
}
x++;
if (y + 1 > w || false != arr[x][y + 1])
return;
y++;
// inner spin
recur(x, y, h - 1, w - 1, arr, list);
return;
}
}