-
Notifications
You must be signed in to change notification settings - Fork 43
/
SpiralMatrix.java
132 lines (118 loc) · 3.43 KB
/
SpiralMatrix.java
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package LeetCodeJava.Array;
// https://leetcode.com/problems/spiral-matrix/description/
import java.util.ArrayList;
import java.util.List;
public class SpiralMatrix {
// V0
// IDEA : array + index op
// https://github.com/yennanliu/CS_basics/blob/master/leetcode_python/Array/spiral-matrix.py
// TODO : implement below
// public List<Integer> spiralOrder(int[][] matrix) {
//
// }
// V0'
// IDEA : array + index op
public List<Integer> spiralOrder_(int[][] matrix) {
int row = matrix.length;
int col = matrix[0].length;
List<Integer> ans = new ArrayList<>();
if(row < 1){
return ans;
}
int startR = 0;
int startCol = 0;
int i = 0;
while(startR <row && startCol<col){
for(i= startCol; i< col; ++i){
ans.add(matrix[startR][i]);
}
startR++;
for(i = startR; i<row; ++i){
ans.add(matrix[i][col-1]);
}
col--;
if(startR < row){
for(i = col-1; i>= startCol;--i){
ans.add(matrix[row-1][i]);
}
row--;
}
if(startCol < col){
for(i = row-1; i>= startR;--i){
ans.add(matrix[i][startCol]);
}
startCol++;
}
}
return ans;
}
// V1
// https://leetcode.com/problems/spiral-matrix/solutions/4700215/easy-solution/
public List<Integer> spiralOrder_1(int[][] m) {
int l=0,r=m[0].length-1,u=0,d=m.length-1;
List<Integer> ll=new ArrayList<>();
while(true){
if(l<=r){
for(int i=l;i<=r;++i){
ll.add(m[u][i]);
}
u++;
}else break;
if(u<=d){
for(int i=u;i<=d;++i){
ll.add(m[i][r]);
}
r--;
}else break;
if(l<=r){
for(int i=r;i>=l;--i){
ll.add(m[d][i]);
}
d--;
}else break;
if(u<=d){
for(int i=d;i>=u;--i){
ll.add(m[i][l]);
}
l++;
}else break;
}
return ll;
}
// V2
// https://leetcode.com/problems/spiral-matrix/solutions/3503095/java-runtime-0-ms-beats-100-memory-40-8-mb-beats-46-17/
public List<Integer> spiralOrder_2(int[][] matrix) {
int row = matrix.length;
List<Integer> ans = new ArrayList<>();
if(row<1){
return ans;
}
int col = matrix[0].length;
int startR = 0;
int startCol = 0;
int i =0;
while(startR<row&& startCol<col){
for(i= startCol; i< col; ++i){
ans.add(matrix[startR][i]);
}
startR++;
for(i = startR; i<row;++i){
ans.add(matrix[i][col-1]);
}
col--;
if(startR<row){
for(i = col-1; i>=startCol;--i){
ans.add(matrix[row-1][i]);
}
row--;
}
if(startCol<col){
for(i = row-1; i>=startR;--i){
ans.add(matrix[i][startCol]);
}
startCol++;
}
}
return ans;
}
}