-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathspiral_matrix.java
More file actions
99 lines (82 loc) · 2.12 KB
/
Copy pathspiral_matrix.java
File metadata and controls
99 lines (82 loc) · 2.12 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
class Solution {
public List<Integer> spiralOrder(int[][] matrix) {
List<Integer> res = new ArrayList<Integer>();
int j=0;
int r = matrix.length;
if(r==0)
{
return res;
}
int cl = matrix[0].length;
int len = r*cl;
int c[] = {0};
if(r==cl && r==1)
{
res.add(matrix[0][0]);
return res;
}
if(cl ==1 )
{
for(int i=0;i<r;i++)
{
res.add(matrix[i][j]);
}
return res;
}
if(r ==1 )
{
j=0;
for(int i=0;i<cl;i++)
{
System.out.print("matrix[0][1]"+i+" "+j);
res.add(matrix[j][i]);
}
return res;
}
int rl=cl-1,ll=0,ul=0,lol=r-1;
while(c[0]>=0)
{
j=ul;
ul=ul+1;
if(c[0] ==len)
break;
for(int i=ll;i<=rl;i++)
{
System.out.print("matrix["+j+"]["+i+"]");
res.add(matrix[j][i]);
++c[0];
}
j=rl;
rl=rl-1;
if(c[0] ==len)
break;
for(int i=ul;i<=lol;i++)
{
System.out.print("matrix["+i+"]["+j+"]");
res.add(matrix[i][j]);
++c[0];
}
j=lol;
lol = lol-1;
if(c[0] ==len)
break;
for(int i=rl;i>=ll;--i)
{
System.out.print(matrix[j][i]);
res.add(matrix[j][i]);
++c[0];
}
j=ll;
ll=ll+1;
if(c[0] ==len)
break;
for(int i=lol;i>=ul;--i)
{
System.out.print(matrix[i][j]);
res.add(matrix[i][j]);
++c[0];
}
}
return res;
}
}