Skip to content

Commit

Permalink
add 54 java, update
Browse files Browse the repository at this point in the history
  • Loading branch information
yennanliu committed Feb 29, 2024
1 parent 7daefb7 commit b27fa64
Show file tree
Hide file tree
Showing 4 changed files with 143 additions and 11 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@
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)
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)
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)
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)
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)
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!)
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)
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)
Expand Down
2 changes: 1 addition & 1 deletion data/progress.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
20240229: 39,48(again),49,53
20240229: 39,48(again),49,53,54
20240228: 20,21,23,33(again)
20240227: 1,3,5,4,19
20231218: 57(todo),58(todo),268,61(stodo),297
Expand Down
18 changes: 9 additions & 9 deletions data/to_review.txt
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
2024-04-24 -> ['39,48(again),49,53']
2024-04-24 -> ['39,48(again),49,53,54']
2024-04-23 -> ['20,21,23,33(again)']
2024-04-22 -> ['1,3,5,4,19']
2024-04-03 -> ['39,48(again),49,53']
2024-04-03 -> ['39,48(again),49,53,54']
2024-04-02 -> ['20,21,23,33(again)']
2024-04-01 -> ['1,3,5,4,19']
2024-03-21 -> ['39,48(again),49,53']
2024-03-21 -> ['39,48(again),49,53,54']
2024-03-20 -> ['20,21,23,33(again)']
2024-03-19 -> ['1,3,5,4,19']
2024-03-13 -> ['39,48(again),49,53']
2024-03-13 -> ['39,48(again),49,53,54']
2024-03-12 -> ['20,21,23,33(again)']
2024-03-11 -> ['1,3,5,4,19']
2024-03-08 -> ['39,48(again),49,53']
2024-03-08 -> ['39,48(again),49,53,54']
2024-03-07 -> ['20,21,23,33(again)']
2024-03-06 -> ['1,3,5,4,19']
2024-03-05 -> ['39,48(again),49,53']
2024-03-05 -> ['39,48(again),49,53,54']
2024-03-04 -> ['20,21,23,33(again)']
2024-03-03 -> ['39,48(again),49,53', '1,3,5,4,19']
2024-03-02 -> ['39,48(again),49,53', '20,21,23,33(again)']
2024-03-01 -> ['39,48(again),49,53', '20,21,23,33(again)', '1,3,5,4,19']
2024-03-03 -> ['39,48(again),49,53,54', '1,3,5,4,19']
2024-03-02 -> ['39,48(again),49,53,54', '20,21,23,33(again)']
2024-03-01 -> ['39,48(again),49,53,54', '20,21,23,33(again)', '1,3,5,4,19']
2024-02-29 -> ['20,21,23,33(again)', '1,3,5,4,19']
2024-02-28 -> ['1,3,5,4,19']
2024-02-11 -> ['57(todo),58(todo),268,61(stodo),297']
Expand Down
132 changes: 132 additions & 0 deletions leetcode_java/src/main/java/LeetCodeJava/Array/SpiralMatrix.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,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;
}

}

0 comments on commit b27fa64

Please sign in to comment.