-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSpiralMatrix.java
31 lines (30 loc) · 1.01 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
import java.util.ArrayList;
public class Solution {
public ArrayList<Integer> spiralOrder(int[][] matrix) {
ArrayList<Integer> results = new ArrayList<Integer>();
int m = matrix.length;
if (m == 0)
return results;
int n = matrix[0].length;
if (n == 0)
return results;
int left = 0, right = n - 1, top = 0, bottom = m - 1;
while (left <= right && top <= bottom) {
for (int j = left; j <= right; j++)
results.add(matrix[top][j]);
for (int i = top + 1; i <= bottom; i++)
results.add(matrix[i][right]);
if (top != bottom)
for (int j = right - 1; j >= left; j--)
results.add(matrix[bottom][j]);
if (left != right)
for (int i = bottom - 1; i > top; i--)
results.add(matrix[i][left]);
left++;
right--;
top++;
bottom--;
}
return results;
}
}