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
51 lines (44 loc) · 1.36 KB
/
Copy pathSpiralMatrix.java
File metadata and controls
51 lines (44 loc) · 1.36 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
// 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
/**
* Use 4 pointer left, right, top & bottom to track the boundaries.
* Reduce the boundaries iteratively.
* Watch for edge case to avoid array out of bound exception
*/
class Solution {
public List<Integer> spiralOrder(int[][] matrix) {
int m = matrix.length;
int n = matrix[0].length;
int left = 0, right = n-1, top = 0, bottom = m-1;
List<Integer> li = new ArrayList<>();
while(left <= right && top <= bottom){
// top row
for (int i = left; i <= right; i++){
li.add(matrix[top][i]);
}
top++;
// right wall
for (int i = top; i <= bottom; i++){
li.add(matrix[i][right]);
}
right--;
// bottom row
if(top <= bottom){
for (int i = right; i >= left; i--){
li.add(matrix[bottom][i]);
}
}
bottom--;
// left wall
if(left <= right){
for (int i = bottom; i >= top; i--){
li.add(matrix[i][left]);
}
}
left++;
}
return li;
}
}