From 622cded4dca4f0760701845efa5909cda7837c69 Mon Sep 17 00:00:00 2001 From: yennanliu Date: Sat, 2 Nov 2024 19:37:23 +0800 Subject: [PATCH] update 26 java --- README.md | 2 +- data/progress.txt | 2 +- data/to_review.txt | 18 ++--- .../RemoveDuplicatesFromSortedArray.java | 67 +++++++++++++++- .../src/main/java/dev/workspace5.java | 80 +++++++++++++++++++ 5 files changed, 155 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index ffef49ad..a658cf53 100644 --- a/README.md +++ b/README.md @@ -207,7 +207,7 @@ 015 | [3 Sum](https://leetcode.com/problems/3sum/) | [Python](./leetcode_python/Array/3sum.py), [Java](./leetcode_java/src/main/java/LeetCodeJava/Array/ThreeSum.java)| _O(n^2)_ | _O(1)_ | Medium | Curated Top 75, good basic, check with `# 001 Two Sum`, `#018 4 sum`, `Two Pointers`, `good basic`,`amazon`,`fb`| OK******* (7) 016 | [3 Sum Closest](https://leetcode.com/problems/3sum-closest/) | [Python](./leetcode_python/Array/3sum-closest.py) | _O(n^2)_ | _O(1)_ | Medium |`Two Pointers`, `basic`, `good trick`,`3 sum`,google, fb, apple, m$, GS, amazon| AGAIN**** (2) 018| [4 Sum](https://leetcode.com/problems/4sum/) | [Python](./leetcode_python/Array/4sum.py) | _O(n^3)_ | _O(1)_ | Medium | `k sum`, `Two Pointers`, check `#016 3 sum`, `# 454 4 SUM II`,good trick, `fb` | AGAIN******** (4) -026 | [Remove Duplicates from Sorted Array](https://leetcode.com/problems/remove-duplicates-from-sorted-array/)| [Python](./leetcode_python/Array/remove-duplicates-from-sorted-array.py), [Scala](./leetcode_scala/Array/removeDuplicates.scala), [Java](./leetcode_java/src/main/java/LeetCodeJava/Array/RemoveDuplicatesFromSortedArray.java) | _O(n)_ | _O(1)_| Easy | `Two Pointers`, `basic`, `good trick`,`M$` ,`fb` | AGAIN*********** (10) +026 | [Remove Duplicates from Sorted Array](https://leetcode.com/problems/remove-duplicates-from-sorted-array/)| [Python](./leetcode_python/Array/remove-duplicates-from-sorted-array.py), [Scala](./leetcode_scala/Array/removeDuplicates.scala), [Java](./leetcode_java/src/main/java/LeetCodeJava/Array/RemoveDuplicatesFromSortedArray.java) | _O(n)_ | _O(1)_| Easy | `Two Pointers`, `basic`, `good trick`,`M$` ,`fb` | AGAIN************** (11) 027 | [Remove Element](https://leetcode.com/problems/remove-element/) | [Python](./leetcode_python/Array/remove-element.py), [Scala](./leetcode_scala/Array/removeElement.scala) | _O(n)_ | _O(1)_ | Easy |`basic`, `amazon`| OK* (2) 031 | [Next Permutation](https://leetcode.com/problems/next-permutation/)| [Python](./leetcode_python/Array/next-permutation.py), [Scala](./leetcode_scala/Array/nextPermutation.scala), [Java](./leetcode_java/src/main/java/LeetCodeJava/Array/NextPermutation.java)| _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) diff --git a/data/progress.txt b/data/progress.txt index 1c9f681d..13c4bb3c 100644 --- a/data/progress.txt +++ b/data/progress.txt @@ -1,4 +1,4 @@ -20241102: 802,1197 +20241102: 802,1197,26 20241027: 855,846 20241026: 932 20241024: 951,792 diff --git a/data/to_review.txt b/data/to_review.txt index 07284c20..a42a41d7 100644 --- a/data/to_review.txt +++ b/data/to_review.txt @@ -1,4 +1,4 @@ -2024-12-27 -> ['802,1197'] +2024-12-27 -> ['802,1197,26'] 2024-12-21 -> ['855,846'] 2024-12-20 -> ['932'] 2024-12-18 -> ['951,792'] @@ -7,7 +7,7 @@ 2024-12-12 -> ['1146'] 2024-12-08 -> ['737'] 2024-12-07 -> ['686,734,737'] -2024-12-06 -> ['802,1197', '353'] +2024-12-06 -> ['802,1197,26', '353'] 2024-12-05 -> ['528,334'] 2024-12-03 -> ['1145'] 2024-11-30 -> ['855,846', '1145,1219'] @@ -15,25 +15,25 @@ 2024-11-27 -> ['951,792', '524,221,889'] 2024-11-26 -> ['743,889'] 2024-11-25 -> ['837'] -2024-11-23 -> ['802,1197', '163,1048', '981'] +2024-11-23 -> ['802,1197,26', '163,1048', '981'] 2024-11-22 -> ['298,729', '1087'] 2024-11-21 -> ['1146'] 2024-11-20 -> ['939'] 2024-11-18 -> ['430'] 2024-11-17 -> ['855,846', '737', '363'] 2024-11-16 -> ['932', '686,734,737', '1032,844,1011'] -2024-11-15 -> ['802,1197', '353', '947'] +2024-11-15 -> ['802,1197,26', '353', '947'] 2024-11-14 -> ['951,792', '528,334'] 2024-11-12 -> ['1145', '753'] 2024-11-11 -> ['727'] -2024-11-10 -> ['802,1197', '163,1048'] +2024-11-10 -> ['802,1197,26', '163,1048'] 2024-11-09 -> ['855,846', '298,729', '1145,1219'] 2024-11-08 -> ['932', '1146'] -2024-11-07 -> ['802,1197'] +2024-11-07 -> ['802,1197,26'] 2024-11-06 -> ['951,792', '524,221,889'] -2024-11-05 -> ['802,1197', '743,889'] -2024-11-04 -> ['802,1197', '855,846', '737', '837', '659'] -2024-11-03 -> ['802,1197', '932', '686,734,737', '801,552'] +2024-11-05 -> ['802,1197,26', '743,889'] +2024-11-04 -> ['802,1197,26', '855,846', '737', '837', '659'] +2024-11-03 -> ['802,1197,26', '932', '686,734,737', '801,552'] 2024-11-02 -> ['163,1048', '353', '981', '1057,1066,1110'] 2024-11-01 -> ['855,846', '951,792', '298,729', '528,334', '1087'] 2024-10-31 -> ['932', '1146'] diff --git a/leetcode_java/src/main/java/LeetCodeJava/Array/RemoveDuplicatesFromSortedArray.java b/leetcode_java/src/main/java/LeetCodeJava/Array/RemoveDuplicatesFromSortedArray.java index 86c4a61e..6b526c8d 100644 --- a/leetcode_java/src/main/java/LeetCodeJava/Array/RemoveDuplicatesFromSortedArray.java +++ b/leetcode_java/src/main/java/LeetCodeJava/Array/RemoveDuplicatesFromSortedArray.java @@ -1,16 +1,78 @@ package LeetCodeJava.Array; // https://leetcode.com/problems/remove-duplicates-from-sorted-array/ - +/** + * 26. Remove Duplicates from Sorted Array + * Solved + * Easy + * Topics + * Companies + * Hint + * Given an integer array nums sorted in non-decreasing order, remove the duplicates in-place such that each unique element appears only once. The relative order of the elements should be kept the same. Then return the number of unique elements in nums. + * + * Consider the number of unique elements of nums to be k, to get accepted, you need to do the following things: + * + * Change the array nums such that the first k elements of nums contain the unique elements in the order they were present in nums initially. The remaining elements of nums are not important as well as the size of nums. + * Return k. + * Custom Judge: + * + * The judge will test your solution with the following code: + * + * int[] nums = [...]; // Input array + * int[] expectedNums = [...]; // The expected answer with correct length + * + * int k = removeDuplicates(nums); // Calls your implementation + * + * assert k == expectedNums.length; + * for (int i = 0; i < k; i++) { + * assert nums[i] == expectedNums[i]; + * } + * If all assertions pass, then your solution will be accepted. + * + * + * + * Example 1: + * + * Input: nums = [1,1,2] + * Output: 2, nums = [1,2,_] + * Explanation: Your function should return k = 2, with the first two elements of nums being 1 and 2 respectively. + * It does not matter what you leave beyond the returned k (hence they are underscores). + * Example 2: + * + * Input: nums = [0,0,1,1,1,2,2,3,3,4] + * Output: 5, nums = [0,1,2,3,4,_,_,_,_,_] + * Explanation: Your function should return k = 5, with the first five elements of nums being 0, 1, 2, 3, and 4 respectively. + * It does not matter what you leave beyond the returned k (hence they are underscores). + * + * + * Constraints: + * + * 1 <= nums.length <= 3 * 104 + * -100 <= nums[i] <= 100 + * nums is sorted in non-decreasing order. + * + * + */ import java.util.LinkedHashSet; public class RemoveDuplicatesFromSortedArray { // V0 + // IDEA : 2 POINTERS + public int removeDuplicates(int[] nums) { + int i = 0; + for (int j = 1; j < nums.length; j++) { + if (nums[i] != nums[j]) { + i++; + nums[i] = nums[j]; + } + } + return i + 1; + } // V1 // https://leetcode.com/problems/remove-duplicates-from-sorted-array/solutions/3416595/c-java-python-javascript-fully-explained-two-pointer/ - public int removeDuplicates(int[] nums) { + public int removeDuplicates_1(int[] nums) { int i = 0; for (int j = 1; j < nums.length; j++) { if (nums[i] != nums[j]) { @@ -59,5 +121,4 @@ public int removeDuplicates_3(int[] nums) { return set.size(); } - } diff --git a/leetcode_java/src/main/java/dev/workspace5.java b/leetcode_java/src/main/java/dev/workspace5.java index 041e69e4..21085358 100644 --- a/leetcode_java/src/main/java/dev/workspace5.java +++ b/leetcode_java/src/main/java/dev/workspace5.java @@ -2248,6 +2248,86 @@ public int minKnightMoves(int x, int y) { return -1; } + // LC 26 + // https://leetcode.com/problems/remove-duplicates-from-sorted-array/description/ + // 7.05 - 7.15 PM + // 2 pointers + /** + * - exp 1 + * + * nums = [1,1,2] + * + * -> [1,1,2] if nums[s] == nums[f], swap nums[s] and nums[f] + * s + * f + * + * [1,1,2] if nums[s] == nums[f], and s != f, move s + * f + * s s + * + * [1,2, 1] if nums[s] != nums[f], swap nums[s] and nums[f] + * f + * s + * + * + * - exp 2 + * + * nums = [0,0,1,1,1,2,2,3,3,4] + * + * + * + * -> [0,0,1,1,1,2,2,3,3,4] if nums[s] != nums[f], swap nums[s] and nums[f] + * s + * f + * + * -> [0,0,1,1,1,2,2,3,3,4] if nums[s] == nums[f], move s + * f + * s s + * + * + * -> [0,1,0,1,1,2,2,3,3,4] if nums[s] != nums[f], swap nums[s] and nums[f] + * s f + * + * + * -> [0,1,0,1,1,2,2,3,3,4] if nums[s] == nums[f], move s + * s f + * s + */ + /** + * [1,1,2] + * s + * f + * + * [1,2,1] + * s s + * f + * + * + * [1,1,2] + * s + * f + * + * + */ + public int removeDuplicates(int[] nums) { + if (nums.length==0){ + return 0; + } + int s = 0; + int f = 1; + + while (f < nums.length){ + if (nums[s] == nums[f]){ + + s += 1; + int tmp = nums[f]; + nums[f] = nums[s]; + nums[s] = tmp; + } + f += 1; + } + return s; + } }