forked from super30admin/Array-1
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSpiralMatrix.java
More file actions
55 lines (42 loc) · 1.42 KB
/
Copy pathSpiralMatrix.java
File metadata and controls
55 lines (42 loc) · 1.42 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
// Time Complexity : O(m*n)
// Space Complexity : O(1)
// Did this code successfully run on Leetcode : Yes
// Three line explanation of solution in plain english
//Take four pointers ->left, top, right, and bottom.
//And navigate left to right, top to bottom, right to left, bottom to top in that order.
//Increase or decrease the pointers accordingly.
// Your code here along with comments explaining your approach
class SpiralMatrix {
public List<Integer> spiralOrder(int[][] matrix) {
List<Integer> result = new ArrayList<>();
int m = matrix.length;
int n = matrix[0].length;
int left = 0;
int top = 0;
int right = n - 1;
int bottom = m - 1;
while (left <= right && top <= bottom) {
for (int j = left; j <= right; j++) {
result.add(matrix[top][j]);
}
top++;
for (int i = top; i <= bottom; i++) {
result.add(matrix[i][right]);
}
right--;
if (top <= bottom) {
for (int j = right; j >= left; j--) {
result.add(matrix[bottom][j]);
}
bottom--;
}
if (left <= right) {
for (int i = bottom; i >= top; i--) {
result.add(matrix[i][left]);
}
left++;
}
}
return result;
}
}