-
Notifications
You must be signed in to change notification settings - Fork 0
/
Spiral Matrix.java
56 lines (45 loc) · 1.43 KB
/
Spiral Matrix.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
/*
Given an m x n matrix, return all elements of the matrix in spiral order.
Example 1:
Input: matrix = [[1,2,3],[4,5,6],[7,8,9]]
Output: [1,2,3,6,9,8,7,4,5]
*/
public class Solution {
public List<Integer> spiralOrder(int[][] matrix) {
List<Integer> res = new ArrayList<Integer>();
if (matrix.length == 0) {
return res;
}
int top = 0; //row begin
int bottom = matrix.length-1; //row end
int left = 0; // column begin
int right = matrix[0].length - 1; //column end
while (top <= bottom && left <= right) {
// Traverse Right
for (int j = left; j <= right; j ++) {
res.add(matrix[top][j]);
}
top++;
// Traverse Down
for (int j = top; j <= bottom; j ++) {
res.add(matrix[j][right]);
}
right--;
if (top <= bottom) {
// Traverse Left
for (int j = right; j >= left; j --) {
res.add(matrix[bottom][j]);
}
}
bottom--;
if (left <= right) {
// Traver Up
for (int j = bottom; j >= top; j --) {
res.add(matrix[j][left]);
}
}
left ++;
}
return res;
}
}