-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathSpiralMatrix.java
More file actions
55 lines (50 loc) · 1.51 KB
/
SpiralMatrix.java
File metadata and controls
55 lines (50 loc) · 1.51 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
// Take a M X N matrix and print the elements in spiral order.
import java.util.*;
import java.util.ArrayList;
public class Solution {
// DO NOT MODIFY THE ARGUMENTS WITH "final" PREFIX. IT IS READ ONLY
public int[] spiralOrder(final int[][] A) {
int size = A.length * A[0].length;
int[] array = new int[size];
List<Integer> list = new ArrayList<Integer>();
int row_start = 0;
int row_end = A.length;
int col_start = 0;
int col_end = A[0].length;
while(row_start < row_end && col_start < col_end)
{
for(int i=col_start; i<col_end; i++)
{
list.add(A[row_start][i]);
}
row_start++;
for(int i=row_start; i<row_end; i++)
{
list.add(A[i][col_end-1]);
}
col_end--;
if(row_start < row_end)
{
for(int i = col_end-1; i>=col_start; i--)
{
list.add(A[row_end-1][i]);
}
row_end--;
}
if(col_start < col_end)
{
for(int i=row_end-1; i>=row_start; i--)
{
list.add(A[i][col_start]);
}
col_start++;
}
}
for(int i=0; i<list.size(); i++)
{
array[i] = list.get(i);
}
//list.toArray(array);
return array;
}
}