-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathP5_18.java
More file actions
64 lines (52 loc) · 1.48 KB
/
P5_18.java
File metadata and controls
64 lines (52 loc) · 1.48 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
import java.util.ArrayList;
import java.util.List;
public class P5_18 {
public static void main(String[] args) {
// TODO Auto-generated method stub
int[][] test = new int[][] { {1,2,3,4},{5,6,7,8},{9,10,11,12}};
int[][] test3 = new int[][] { {7},{9}, {6}};
int[][] test2 = new int[][] { {1,2,3},{4,5,6},{7,8,9}};
System.out.println(spiralOrder(test3).toString());
System.out.println(spiralOrder(test2).toString());
}
public static List<Integer> spiralOrder(int[][] array)
{
if(array.length == 0)
return new ArrayList<Integer>();
int width = array[0].length;
int height = array.length;
return spiralRecur(array,0,0,width, height);
}
public static List<Integer> spiralRecur(int[][] arr, int x, int y, int width, int height)
{
List<Integer> ret = new ArrayList<Integer>();
//Check base Case
if(width <= 0 || height <= 0)
return ret;
//x,y is the position of the top left of the array we are examining;
//Generate the spiral border
for(int i = x; i < x + width; i++)
{
ret.add(arr[y][i]);
}
//Generate the right side
for(int i = y+1; i < y + height -1; i++)
{
ret.add(arr[i][x + width -1]);
}
//Generate the bottom
for(int i = x + width -1; i > x; i--)
{
if(y + height -1 != y)
ret.add(arr[y + height -1][i]);
}
//Generate the left side
for(int i = y + height -1; i > y; i--)
{
if(i != y)
ret.add(arr[i][x]);
}
ret.addAll(spiralRecur(arr, x +1, y+1, width -2, height -2));
return ret;
}
}