From b27fa6430b6668288bd88eba3d374c0412b653fe Mon Sep 17 00:00:00 2001 From: yennanliu Date: Thu, 29 Feb 2024 23:29:58 +0800 Subject: [PATCH] add 54 java, update --- README.md | 2 +- data/progress.txt | 2 +- data/to_review.txt | 18 +-- .../java/LeetCodeJava/Array/SpiralMatrix.java | 132 ++++++++++++++++++ 4 files changed, 143 insertions(+), 11 deletions(-) create mode 100644 leetcode_java/src/main/java/LeetCodeJava/Array/SpiralMatrix.java diff --git a/README.md b/README.md index 4e5286e5..522546bd 100644 --- a/README.md +++ b/README.md @@ -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) diff --git a/data/progress.txt b/data/progress.txt index 9b99f612..2f811679 100644 --- a/data/progress.txt +++ b/data/progress.txt @@ -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 diff --git a/data/to_review.txt b/data/to_review.txt index ff95b6c5..1aca73a9 100644 --- a/data/to_review.txt +++ b/data/to_review.txt @@ -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'] diff --git a/leetcode_java/src/main/java/LeetCodeJava/Array/SpiralMatrix.java b/leetcode_java/src/main/java/LeetCodeJava/Array/SpiralMatrix.java new file mode 100644 index 00000000..66e02bd0 --- /dev/null +++ b/leetcode_java/src/main/java/LeetCodeJava/Array/SpiralMatrix.java @@ -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 spiralOrder(int[][] matrix) { +// +// } + + // V0' + // IDEA : array + index op + public List spiralOrder_(int[][] matrix) { + + int row = matrix.length; + int col = matrix[0].length; + + List ans = new ArrayList<>(); + + if(row < 1){ + return ans; + } + + int startR = 0; + int startCol = 0; + int i = 0; + + while(startR = 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 spiralOrder_1(int[][] m) { + int l=0,r=m[0].length-1,u=0,d=m.length-1; + List 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 spiralOrder_2(int[][] matrix) { + int row = matrix.length; + List ans = new ArrayList<>(); + if(row<1){ + return ans; + } + int col = matrix[0].length; + int startR = 0; + int startCol = 0; + int i =0; + + while(startR=startCol;--i){ + ans.add(matrix[row-1][i]); + } + row--; + } + if(startCol=startR;--i){ + ans.add(matrix[i][startCol]); + } + startCol++; + } + } + return ans; + } + +}