Skip to content

Commit 622cded

Browse files
committed
update 26 java
1 parent a8a4ffb commit 622cded

File tree

5 files changed

+155
-14
lines changed

5 files changed

+155
-14
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@
207207
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)
208208
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)
209209
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)
210-
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)
210+
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)
211211
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)
212212
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)
213213
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)

data/progress.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
20241102: 802,1197
1+
20241102: 802,1197,26
22
20241027: 855,846
33
20241026: 932
44
20241024: 951,792

data/to_review.txt

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
2024-12-27 -> ['802,1197']
1+
2024-12-27 -> ['802,1197,26']
22
2024-12-21 -> ['855,846']
33
2024-12-20 -> ['932']
44
2024-12-18 -> ['951,792']
@@ -7,33 +7,33 @@
77
2024-12-12 -> ['1146']
88
2024-12-08 -> ['737']
99
2024-12-07 -> ['686,734,737']
10-
2024-12-06 -> ['802,1197', '353']
10+
2024-12-06 -> ['802,1197,26', '353']
1111
2024-12-05 -> ['528,334']
1212
2024-12-03 -> ['1145']
1313
2024-11-30 -> ['855,846', '1145,1219']
1414
2024-11-29 -> ['932']
1515
2024-11-27 -> ['951,792', '524,221,889']
1616
2024-11-26 -> ['743,889']
1717
2024-11-25 -> ['837']
18-
2024-11-23 -> ['802,1197', '163,1048', '981']
18+
2024-11-23 -> ['802,1197,26', '163,1048', '981']
1919
2024-11-22 -> ['298,729', '1087']
2020
2024-11-21 -> ['1146']
2121
2024-11-20 -> ['939']
2222
2024-11-18 -> ['430']
2323
2024-11-17 -> ['855,846', '737', '363']
2424
2024-11-16 -> ['932', '686,734,737', '1032,844,1011']
25-
2024-11-15 -> ['802,1197', '353', '947']
25+
2024-11-15 -> ['802,1197,26', '353', '947']
2626
2024-11-14 -> ['951,792', '528,334']
2727
2024-11-12 -> ['1145', '753']
2828
2024-11-11 -> ['727']
29-
2024-11-10 -> ['802,1197', '163,1048']
29+
2024-11-10 -> ['802,1197,26', '163,1048']
3030
2024-11-09 -> ['855,846', '298,729', '1145,1219']
3131
2024-11-08 -> ['932', '1146']
32-
2024-11-07 -> ['802,1197']
32+
2024-11-07 -> ['802,1197,26']
3333
2024-11-06 -> ['951,792', '524,221,889']
34-
2024-11-05 -> ['802,1197', '743,889']
35-
2024-11-04 -> ['802,1197', '855,846', '737', '837', '659']
36-
2024-11-03 -> ['802,1197', '932', '686,734,737', '801,552']
34+
2024-11-05 -> ['802,1197,26', '743,889']
35+
2024-11-04 -> ['802,1197,26', '855,846', '737', '837', '659']
36+
2024-11-03 -> ['802,1197,26', '932', '686,734,737', '801,552']
3737
2024-11-02 -> ['163,1048', '353', '981', '1057,1066,1110']
3838
2024-11-01 -> ['855,846', '951,792', '298,729', '528,334', '1087']
3939
2024-10-31 -> ['932', '1146']

leetcode_java/src/main/java/LeetCodeJava/Array/RemoveDuplicatesFromSortedArray.java

Lines changed: 64 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,78 @@
11
package LeetCodeJava.Array;
22

33
// https://leetcode.com/problems/remove-duplicates-from-sorted-array/
4-
4+
/**
5+
* 26. Remove Duplicates from Sorted Array
6+
* Solved
7+
* Easy
8+
* Topics
9+
* Companies
10+
* Hint
11+
* 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.
12+
*
13+
* Consider the number of unique elements of nums to be k, to get accepted, you need to do the following things:
14+
*
15+
* 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.
16+
* Return k.
17+
* Custom Judge:
18+
*
19+
* The judge will test your solution with the following code:
20+
*
21+
* int[] nums = [...]; // Input array
22+
* int[] expectedNums = [...]; // The expected answer with correct length
23+
*
24+
* int k = removeDuplicates(nums); // Calls your implementation
25+
*
26+
* assert k == expectedNums.length;
27+
* for (int i = 0; i < k; i++) {
28+
* assert nums[i] == expectedNums[i];
29+
* }
30+
* If all assertions pass, then your solution will be accepted.
31+
*
32+
*
33+
*
34+
* Example 1:
35+
*
36+
* Input: nums = [1,1,2]
37+
* Output: 2, nums = [1,2,_]
38+
* Explanation: Your function should return k = 2, with the first two elements of nums being 1 and 2 respectively.
39+
* It does not matter what you leave beyond the returned k (hence they are underscores).
40+
* Example 2:
41+
*
42+
* Input: nums = [0,0,1,1,1,2,2,3,3,4]
43+
* Output: 5, nums = [0,1,2,3,4,_,_,_,_,_]
44+
* Explanation: Your function should return k = 5, with the first five elements of nums being 0, 1, 2, 3, and 4 respectively.
45+
* It does not matter what you leave beyond the returned k (hence they are underscores).
46+
*
47+
*
48+
* Constraints:
49+
*
50+
* 1 <= nums.length <= 3 * 104
51+
* -100 <= nums[i] <= 100
52+
* nums is sorted in non-decreasing order.
53+
*
54+
*
55+
*/
556
import java.util.LinkedHashSet;
657

758
public class RemoveDuplicatesFromSortedArray {
859

960
// V0
61+
// IDEA : 2 POINTERS
62+
public int removeDuplicates(int[] nums) {
63+
int i = 0;
64+
for (int j = 1; j < nums.length; j++) {
65+
if (nums[i] != nums[j]) {
66+
i++;
67+
nums[i] = nums[j];
68+
}
69+
}
70+
return i + 1;
71+
}
1072

1173
// V1
1274
// https://leetcode.com/problems/remove-duplicates-from-sorted-array/solutions/3416595/c-java-python-javascript-fully-explained-two-pointer/
13-
public int removeDuplicates(int[] nums) {
75+
public int removeDuplicates_1(int[] nums) {
1476
int i = 0;
1577
for (int j = 1; j < nums.length; j++) {
1678
if (nums[i] != nums[j]) {
@@ -59,5 +121,4 @@ public int removeDuplicates_3(int[] nums) {
59121
return set.size();
60122
}
61123

62-
63124
}

leetcode_java/src/main/java/dev/workspace5.java

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2248,6 +2248,86 @@ public int minKnightMoves(int x, int y) {
22482248
return -1;
22492249
}
22502250

2251+
// LC 26
2252+
// https://leetcode.com/problems/remove-duplicates-from-sorted-array/description/
2253+
// 7.05 - 7.15 PM
2254+
// 2 pointers
2255+
/**
2256+
* - exp 1
2257+
*
2258+
* nums = [1,1,2]
2259+
*
2260+
* -> [1,1,2] if nums[s] == nums[f], swap nums[s] and nums[f]
2261+
* s
2262+
* f
2263+
*
2264+
* [1,1,2] if nums[s] == nums[f], and s != f, move s
2265+
* f
2266+
* s s
2267+
*
2268+
* [1,2, 1] if nums[s] != nums[f], swap nums[s] and nums[f]
2269+
* f
2270+
* s
2271+
*
2272+
*
2273+
* - exp 2
2274+
*
2275+
* nums = [0,0,1,1,1,2,2,3,3,4]
2276+
*
2277+
*
2278+
*
2279+
* -> [0,0,1,1,1,2,2,3,3,4] if nums[s] != nums[f], swap nums[s] and nums[f]
2280+
* s
2281+
* f
2282+
*
2283+
* -> [0,0,1,1,1,2,2,3,3,4] if nums[s] == nums[f], move s
2284+
* f
2285+
* s s
2286+
*
2287+
*
2288+
* -> [0,1,0,1,1,2,2,3,3,4] if nums[s] != nums[f], swap nums[s] and nums[f]
2289+
* s f
2290+
*
2291+
*
2292+
* -> [0,1,0,1,1,2,2,3,3,4] if nums[s] == nums[f], move s
2293+
* s f
2294+
* s
2295+
*/
2296+
/**
2297+
* [1,1,2]
2298+
* s
2299+
* f
2300+
*
2301+
* [1,2,1]
2302+
* s s
2303+
* f
2304+
*
2305+
*
2306+
* [1,1,2]
2307+
* s
2308+
* f
2309+
*
2310+
*
2311+
*/
2312+
public int removeDuplicates(int[] nums) {
2313+
if (nums.length==0){
2314+
return 0;
2315+
}
2316+
int s = 0;
2317+
int f = 1;
2318+
2319+
while (f < nums.length){
2320+
if (nums[s] == nums[f]){
2321+
2322+
s += 1;
2323+
int tmp = nums[f];
2324+
nums[f] = nums[s];
2325+
nums[s] = tmp;
2326+
}
2327+
f += 1;
2328+
}
2329+
return s;
2330+
}
22512331

22522332
}
22532333

0 commit comments

Comments
 (0)