-
Notifications
You must be signed in to change notification settings - Fork 0
/
SpiralMatrix.java
48 lines (41 loc) · 1.4 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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import java.util.ArrayList;
import java.util.List;
class SetZeroMatrix {
public static void main(String[] args) {
// int[][] matrix = {{1,2,3},{1,0,1},{1,1,1}};
int[][] matrix1 = {{0, 1, 2, 0}, {3, 4, 5, 2}, {1, 3, 1, 5}};
System.out.println(spiralOrder(matrix1));
}
public static List<Integer> spiralOrder(int[][] matrix) {
List<Integer> answer = new ArrayList<>();
int m = matrix.length, n = matrix[0].length;
int top = 0, bottom = m - 1, left = 0, right = n - 1;
while (top <= bottom && left <= right) {
/** Left to Right Traversal* */
for (int i = left; i <= right; i++) {
answer.add(matrix[left][i]);
}
top++;
/** Top to Bottom Traversal* */
for (int i = top; i <= bottom; i++) {
answer.add(matrix[i][right]);
}
right--;
/** Right to Left Traversal* */
if(top<=bottom){
for (int i = right; i >= left; i--) {
answer.add(matrix[bottom][i]);
}
bottom--;
}
/** Bottom to Top Traversal* */
if(left<=right){
for (int i = bottom; i >= top; i--) {
answer.add(matrix[i][left]);
}
left++;
}
}
return answer;
}
}