-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMatrix.java
More file actions
42 lines (37 loc) · 1.21 KB
/
Matrix.java
File metadata and controls
42 lines (37 loc) · 1.21 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
import java.util.*;
public class Matrix{
//Spiral traversal
//Given an m x n matrix, return all elements of the matrix in spiral order.
// time complexity O(m*n)
// space complexity O(1) because the algorithm uses only constant extra
//space, and the output space is not counted in auxiliary space.
public static List<Integer> spiralOrder(int[][] matrix) {
ArrayList<Integer> res = new ArrayList<>();
int rs =0, re = matrix.length-1 , cs = 0 , ce = matrix[0].length-1;
while(rs <= re && cs <= ce){
for(int i=cs; i<=ce; i++){
res.add(matrix[rs][i]);
}
rs++;
if(rs > re) return res;
for(int i =rs; i<= re; i++){
res.add(matrix[i][ce]);
}
ce--;
if(cs > ce) return res;
for(int i = ce; i>=cs; i--){
res.add(matrix[re][i]);
}
re--;
if(rs > re) return res;
for(int i = re; i>= rs; i--){
res.add(matrix[i][cs]);
}
cs++;
if(cs > ce) return res;
}
return res;
}
public static void main(String args[]){
}
}