-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSpiralMatrix.java
More file actions
48 lines (39 loc) · 854 Bytes
/
SpiralMatrix.java
File metadata and controls
48 lines (39 loc) · 854 Bytes
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
import java.util.*;
public class SpiralMatrix {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
}
public List<Integer> spiralOrder(int[][] matrix) {
List<Integer> list = new ArrayList<Integer>();
int m = matrix.length;
if(m == 0) return list;
int n = matrix[0].length;
int row = 0;
int col = 0;
while(2*row < m && 2*col <n)
{
for(int i = col ; i<= n-col-1 ; i++)
{
list.add(matrix[row][i]);
}
for(int i = row + 1; i <= m- row-1; i++)
{
list.add(matrix[i][n-col-1]);
}
for(int i = n-col -2; i>=col&& m-row-1 > row ; i--)
{
list.add(matrix[m-row-1][i]);
}
for(int i = m-row-2; i>= row+1 && n-col-1>col; i--)
{
list.add(matrix[i][col]);
}
row++;
col++;
}
return list;
}
}