-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSpiralMatrixOne.java
57 lines (52 loc) · 1.75 KB
/
SpiralMatrixOne.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
57
package arrays.medium;
import java.util.ArrayList;
import java.util.List;
/**
* Problem 54 in <a href="https://leetcode.com/problems/spiral-matrix/">Leetcode</a>
* 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]
*
* Example 2:
* Input: matrix = [[1,2,3,4],[5,6,7,8],[9,10,11,12]]
* Output: [1,2,3,4,8,12,11,10,9,5,6,7]
*/
public class SpiralMatrixOne {
public static void main(String[] args) {
int[][] matrix = {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}};
System.out.println(simulation(matrix));
}
private static List<Integer> simulation(int[][] matrix) {
List<Integer> list = new ArrayList<>();
int rows = matrix.length, columns = matrix[0].length;
int left = 0, right = columns - 1, top = 0, down = rows - 1;
int direction = 0;
while (left <= right && top <= down) {
if (direction == 0) {
for (int i = left; i <= right; i++) {
list.add(matrix[top][i]);
}
top++;
} else if (direction == 1) {
for (int i = top; i <= down; i++) {
list.add(matrix[i][right]);
}
right--;
} else if (direction == 2) {
for (int i = right; i >= left; i--) {
list.add(matrix[down][i]);
}
down--;
} else if (direction == 3) {
for (int i = down; i >= top; i--) {
list.add(matrix[i][left]);
}
left++;
}
direction = (direction + 1) % 4;
}
return list;
}
}