-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSpiralMatrix.java
More file actions
66 lines (62 loc) · 1.81 KB
/
SpiralMatrix.java
File metadata and controls
66 lines (62 loc) · 1.81 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
56
57
58
59
60
61
62
63
64
65
66
<<<<<<< HEAD
class Solution {
public List<Integer> spiralOrder(int[][] matrix) {
List<Integer> l1 = new ArrayList<Integer>();
//left
int l=0,b=matrix.length-1,r=matrix[0].length-1,t=0;
while(l<=r || t<=b){
if(t<=b){
for(int i=l;i<=r;i++)
l1.add(matrix[t][i]);
t++;
}
if(l<=r){
for(int i=t;i<=b;i++)
l1.add(matrix[i][r]);
r--;
}
if(t<=b){
for(int i=r;i>=l;i--)
l1.add(matrix[b][i]);
b--;
}
if(l<=r){
for(int i=b;i>=t;i--)
l1.add(matrix[i][l]);
l++;
}
}
return l1;
}
=======
class Solution {
public List<Integer> spiralOrder(int[][] matrix) {
List<Integer> l1 = new ArrayList<Integer>();
//left
int l=0,b=matrix.length-1,r=matrix[0].length-1,t=0;
while(l<=r || t<=b){
if(t<=b){
for(int i=l;i<=r;i++)
l1.add(matrix[t][i]);
t++;
}
if(l<=r){
for(int i=t;i<=b;i++)
l1.add(matrix[i][r]);
r--;
}
if(t<=b){
for(int i=r;i>=l;i--)
l1.add(matrix[b][i]);
b--;
}
if(l<=r){
for(int i=b;i>=t;i--)
l1.add(matrix[i][l]);
l++;
}
}
return l1;
}
>>>>>>> 622c86d59ac7148ac8e767904db50c6179b2fd40
}