Skip to content

Commit b27fa64

Browse files
committed
add 54 java, update
1 parent 7daefb7 commit b27fa64

File tree

4 files changed

+143
-11
lines changed

4 files changed

+143
-11
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@
217217
031 | [Next Permutation](https://leetcode.com/problems/next-permutation/)| [Python](./leetcode_python/Array/next-permutation.py), [Scala](./leetcode_scala/Array/nextPermutation.scala)| _O(n)_ | _O(1)_ | Medium | good trick, check, 2 pointers, Uber, GS, `google`,`amazon`, `fb` | AGAIN****************** (8) (MUST)
218218
041 | [First Missing Positive](https://leetcode.com/problems/first-missing-positive/)| [Python](./leetcode_python/Array/first-missing-positive.py)| _O(n)_ | _O(1)_ | Hard | good trick, hash key , array, LC top 100 like, `amazon`, `fb`, google, apple, uber | AGAIN******** (2)
219219
048 | [Rotate Image](https://leetcode.com/problems/rotate-image/) | [Python](./leetcode_python/Array/rotate-image.py), [Java](./leetcode_java/src/main/java/LeetCodeJava/Array/RotateImage.java) | _O(n^2)_ | _O(1)_| Medium |Curated Top 75, `basic`, `i,j->j,i` `transpose matrix`, garena, `amazon`, `apple`| AGAIN************* (5) (MUST)
220-
054 | [Spiral Matrix](https://leetcode.com/problems/spiral-matrix/) | [Python](./leetcode_python/Array/spiral-matrix.py) | _O(m * n)_ | _O(1)_ | Medium |Curated Top 75, good basic, array boundary, matrix,`amazon`| AGAIN**** (4)
220+
054 | [Spiral Matrix](https://leetcode.com/problems/spiral-matrix/) | [Python](./leetcode_python/Array/spiral-matrix.py), [Java](./leetcode_java/src/main/java/LeetCodeJava/Array/SpiralMatrix.java) | _O(m * n)_ | _O(1)_ | Medium |Curated Top 75, good basic, array boundary, matrix,`amazon`| AGAIN**** (4)
221221
057 | [Insert Interval](https://leetcode.com/problems/insert-interval/) | [Python](./leetcode_python/Array/insert-interval.py) | | | Medium | Curated Top 75, good basic, `056 Merge Intervals`| AGAIN********** (4) (MUST!)
222222
059 | [Spiral Matrix II](https://leetcode.com/problems/spiral-matrix-ii/) | [Python](./leetcode_python/Array/spiral-matrix-ii.py) | _O(n^2)_ | _O(1)_ | Medium |check `# 054 Spiral Matrix`, `amazon`| AGAIN** (2)
223223
066 | [Plus One](https://leetcode.com/problems/plus-one/) | [Python](./leetcode_python/Array/plus-one.py), [Java](./leetcode_java/src/main/java/LeetCodeJava/Array/PlusOne.java)| _O(n)_ | _O(1)_ | Easy |`basic`, `digit`, `google`| AGAIN** (3)

data/progress.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
20240229: 39,48(again),49,53
1+
20240229: 39,48(again),49,53,54
22
20240228: 20,21,23,33(again)
33
20240227: 1,3,5,4,19
44
20231218: 57(todo),58(todo),268,61(stodo),297

data/to_review.txt

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,23 @@
1-
2024-04-24 -> ['39,48(again),49,53']
1+
2024-04-24 -> ['39,48(again),49,53,54']
22
2024-04-23 -> ['20,21,23,33(again)']
33
2024-04-22 -> ['1,3,5,4,19']
4-
2024-04-03 -> ['39,48(again),49,53']
4+
2024-04-03 -> ['39,48(again),49,53,54']
55
2024-04-02 -> ['20,21,23,33(again)']
66
2024-04-01 -> ['1,3,5,4,19']
7-
2024-03-21 -> ['39,48(again),49,53']
7+
2024-03-21 -> ['39,48(again),49,53,54']
88
2024-03-20 -> ['20,21,23,33(again)']
99
2024-03-19 -> ['1,3,5,4,19']
10-
2024-03-13 -> ['39,48(again),49,53']
10+
2024-03-13 -> ['39,48(again),49,53,54']
1111
2024-03-12 -> ['20,21,23,33(again)']
1212
2024-03-11 -> ['1,3,5,4,19']
13-
2024-03-08 -> ['39,48(again),49,53']
13+
2024-03-08 -> ['39,48(again),49,53,54']
1414
2024-03-07 -> ['20,21,23,33(again)']
1515
2024-03-06 -> ['1,3,5,4,19']
16-
2024-03-05 -> ['39,48(again),49,53']
16+
2024-03-05 -> ['39,48(again),49,53,54']
1717
2024-03-04 -> ['20,21,23,33(again)']
18-
2024-03-03 -> ['39,48(again),49,53', '1,3,5,4,19']
19-
2024-03-02 -> ['39,48(again),49,53', '20,21,23,33(again)']
20-
2024-03-01 -> ['39,48(again),49,53', '20,21,23,33(again)', '1,3,5,4,19']
18+
2024-03-03 -> ['39,48(again),49,53,54', '1,3,5,4,19']
19+
2024-03-02 -> ['39,48(again),49,53,54', '20,21,23,33(again)']
20+
2024-03-01 -> ['39,48(again),49,53,54', '20,21,23,33(again)', '1,3,5,4,19']
2121
2024-02-29 -> ['20,21,23,33(again)', '1,3,5,4,19']
2222
2024-02-28 -> ['1,3,5,4,19']
2323
2024-02-11 -> ['57(todo),58(todo),268,61(stodo),297']
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
package LeetCodeJava.Array;
2+
3+
// https://leetcode.com/problems/spiral-matrix/description/
4+
5+
import java.util.ArrayList;
6+
import java.util.List;
7+
8+
public class SpiralMatrix {
9+
10+
// V0
11+
// IDEA : array + index op
12+
// https://github.com/yennanliu/CS_basics/blob/master/leetcode_python/Array/spiral-matrix.py
13+
// TODO : implement below
14+
// public List<Integer> spiralOrder(int[][] matrix) {
15+
//
16+
// }
17+
18+
// V0'
19+
// IDEA : array + index op
20+
public List<Integer> spiralOrder_(int[][] matrix) {
21+
22+
int row = matrix.length;
23+
int col = matrix[0].length;
24+
25+
List<Integer> ans = new ArrayList<>();
26+
27+
if(row < 1){
28+
return ans;
29+
}
30+
31+
int startR = 0;
32+
int startCol = 0;
33+
int i = 0;
34+
35+
while(startR <row && startCol<col){
36+
for(i= startCol; i< col; ++i){
37+
ans.add(matrix[startR][i]);
38+
}
39+
startR++;
40+
for(i = startR; i<row; ++i){
41+
ans.add(matrix[i][col-1]);
42+
}
43+
col--;
44+
if(startR < row){
45+
for(i = col-1; i>= startCol;--i){
46+
ans.add(matrix[row-1][i]);
47+
}
48+
row--;
49+
}
50+
if(startCol < col){
51+
for(i = row-1; i>= startR;--i){
52+
ans.add(matrix[i][startCol]);
53+
}
54+
startCol++;
55+
}
56+
}
57+
return ans;
58+
}
59+
60+
// V1
61+
// https://leetcode.com/problems/spiral-matrix/solutions/4700215/easy-solution/
62+
public List<Integer> spiralOrder_1(int[][] m) {
63+
int l=0,r=m[0].length-1,u=0,d=m.length-1;
64+
List<Integer> ll=new ArrayList<>();
65+
while(true){
66+
if(l<=r){
67+
for(int i=l;i<=r;++i){
68+
ll.add(m[u][i]);
69+
}
70+
u++;
71+
}else break;
72+
if(u<=d){
73+
for(int i=u;i<=d;++i){
74+
ll.add(m[i][r]);
75+
}
76+
r--;
77+
}else break;
78+
if(l<=r){
79+
for(int i=r;i>=l;--i){
80+
ll.add(m[d][i]);
81+
}
82+
d--;
83+
}else break;
84+
if(u<=d){
85+
for(int i=d;i>=u;--i){
86+
ll.add(m[i][l]);
87+
}
88+
l++;
89+
}else break;
90+
}
91+
return ll;
92+
}
93+
94+
// V2
95+
// https://leetcode.com/problems/spiral-matrix/solutions/3503095/java-runtime-0-ms-beats-100-memory-40-8-mb-beats-46-17/
96+
public List<Integer> spiralOrder_2(int[][] matrix) {
97+
int row = matrix.length;
98+
List<Integer> ans = new ArrayList<>();
99+
if(row<1){
100+
return ans;
101+
}
102+
int col = matrix[0].length;
103+
int startR = 0;
104+
int startCol = 0;
105+
int i =0;
106+
107+
while(startR<row&& startCol<col){
108+
for(i= startCol; i< col; ++i){
109+
ans.add(matrix[startR][i]);
110+
}
111+
startR++;
112+
for(i = startR; i<row;++i){
113+
ans.add(matrix[i][col-1]);
114+
}
115+
col--;
116+
if(startR<row){
117+
for(i = col-1; i>=startCol;--i){
118+
ans.add(matrix[row-1][i]);
119+
}
120+
row--;
121+
}
122+
if(startCol<col){
123+
for(i = row-1; i>=startR;--i){
124+
ans.add(matrix[i][startCol]);
125+
}
126+
startCol++;
127+
}
128+
}
129+
return ans;
130+
}
131+
132+
}

0 commit comments

Comments
 (0)