Skip to content

Commit 776dfa5

Browse files
committed
add 27 java, update cheatsheet
1 parent 128faa5 commit 776dfa5

File tree

5 files changed

+362
-13
lines changed

5 files changed

+362
-13
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -207,8 +207,8 @@
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************** (11)
211-
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)
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 | MUST, `Two Pointers`, `basic`, `good trick`,`M$` ,`fb` | AGAIN************** (11)
211+
027 | [Remove Element](https://leetcode.com/problems/remove-element/) | [Python](./leetcode_python/Array/remove-element.py), [Java](./leetcode_java/src/main/java/LeetCodeJava/Array/RemoveElement.java) | _O(n)_ | _O(1)_ | Easy |MUST, `2 pointers`, `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)
214214
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)

data/progress.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
20241103: 26,27
1+
20241103:
2+
20241102: 26,27
23
20241102: 802,1197,26
34
20241027: 855,846
45
20241026: 932

doc/cheatsheet/2_pointers.md

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,54 @@
4646
// java
4747
// LC 26 (LC 83)
4848
// https://labuladong.online/algo/essential-technique/array-two-pointers-summary/#%E5%8E%9F%E5%9C%B0%E4%BF%AE%E6%94%B9
49+
/**
50+
* //--------------------------------
51+
* Example 1
52+
* //--------------------------------
53+
*
54+
* nums = [1,1,2]
55+
*
56+
* [1,1,2]
57+
* s f
58+
*
59+
* [1,2, 1] if nums[f] != nums[s], move s, then swap f, s
60+
* s s f
61+
*
62+
*
63+
* //--------------------------------
64+
* Example 2
65+
* //--------------------------------
66+
*
67+
* nums = [0,0,1,1,1,2,2,3,3,4]
68+
*
69+
* [0,0,1,1,1,2,2,3,3,4]
70+
* s f
71+
*
72+
* [0,1,0,1,1,2,2,3,3,4] if nums[f] != nums[s], move s, then swap f, s
73+
* s s f
74+
*
75+
* [0,1,0,1,1,2,2,3,3,4]
76+
* s f
77+
*
78+
* [0,1,0,1,1,2,2,3,3,4]
79+
* s f
80+
*
81+
* [0,1,2,1,1,0,2,3,3,4] if nums[f] != nums[s], move s, then swap f, s
82+
* s s f
83+
*
84+
* [0,1,2,1,1,0,2,3,3,4]
85+
* s f
86+
*
87+
* [0,1,2,3,1,0,2,1,3,4] if nums[f] != nums[s], move s, then swap f, s
88+
* s s f
89+
*
90+
* [0,1,2,3,1,0,2,1,3,4]
91+
* s f
92+
*
93+
* [0,1,2,3,4,0,2,1,3,1] if nums[f] != nums[s], move s, then swap f, s
94+
* s s f
95+
*
96+
*/
4997
class Solution {
5098
public int removeDuplicates(int[] nums) {
5199
if (nums.length == 0) {
@@ -73,6 +121,69 @@ class Solution {
73121
// java
74122
// LC 27
75123
// https://labuladong.online/algo/essential-technique/array-two-pointers-summary/#%E5%8E%9F%E5%9C%B0%E4%BF%AE%E6%94%B9
124+
/**
125+
* //--------------------
126+
* Example 1
127+
* //--------------------
128+
*
129+
* nums = [3,2,2,3], val = 3
130+
*
131+
* [3,2,2,3]
132+
* s
133+
* f
134+
*
135+
* [2,3,2,3] if nums[f] != val, swap, move s
136+
* s s
137+
* f
138+
*
139+
* [2,2,3,3] if nums[f] != val, swap, move s
140+
* s s
141+
* f
142+
*
143+
* [2,2,3,3]
144+
* s
145+
* f
146+
*
147+
*
148+
* //--------------------
149+
* Example 2
150+
* //--------------------
151+
*
152+
* nums = [0,1,2,2,3,0,4,2], val = 2
153+
*
154+
*
155+
* [0,1,2,2,3,0,4,2] if nums[f] != val, swap, move s
156+
* s s
157+
* f
158+
*
159+
* [0,1,2,2,3,0,4,2] if nums[f] != val, swap, move s
160+
* s s
161+
* f
162+
*
163+
* [0,1,2,2,3,0,4,2]
164+
* s
165+
* f
166+
*
167+
* [0,1,2,2,3,0,4,2]
168+
* s
169+
* f
170+
*
171+
* [0,1,3,2,2,0,4,2] if nums[f] != val, swap, move s
172+
* s s
173+
* f
174+
*
175+
* [0,1,3,0,2,2,4,2] if nums[f] != val, swap, move s
176+
* s s
177+
* f
178+
*
179+
* [0,1,3,0,4,2,2,2] if nums[f] != val, swap, move s
180+
* s s
181+
* f
182+
*
183+
* [0,1,3,0,4,2,2,2]
184+
* s
185+
* f
186+
*/
76187
class Solution {
77188
public int removeElement(int[] nums, int val) {
78189
int fast = 0, slow = 0;
Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
1+
package LeetCodeJava.Array;
2+
3+
// https://leetcode.com/problems/remove-element/description/
4+
/**
5+
* 27. Remove Element
6+
Solved
7+
Easy
8+
Topics
9+
Companies
10+
Hint
11+
Given an integer array nums and an integer val, remove all occurrences of val in nums in-place. The order of the elements may be changed. Then return the number of elements in nums which are not equal to val.
12+
13+
Consider the number of elements in nums which are not equal to val 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 elements which are not equal to val. 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 val = ...; // Value to remove
23+
int[] expectedNums = [...]; // The expected answer with correct length.
24+
// It is sorted with no values equaling val.
25+
26+
int k = removeElement(nums, val); // Calls your implementation
27+
28+
assert k == expectedNums.length;
29+
sort(nums, 0, k); // Sort the first k elements of nums
30+
for (int i = 0; i < actualLength; i++) {
31+
assert nums[i] == expectedNums[i];
32+
}
33+
If all assertions pass, then your solution will be accepted.
34+
35+
36+
37+
Example 1:
38+
39+
Input: nums = [3,2,2,3], val = 3
40+
Output: 2, nums = [2,2,_,_]
41+
Explanation: Your function should return k = 2, with the first two elements of nums being 2.
42+
It does not matter what you leave beyond the returned k (hence they are underscores).
43+
Example 2:
44+
45+
Input: nums = [0,1,2,2,3,0,4,2], val = 2
46+
Output: 5, nums = [0,1,4,0,3,_,_,_]
47+
Explanation: Your function should return k = 5, with the first five elements of nums containing 0, 0, 1, 3, and 4.
48+
Note that the five elements can be returned in any order.
49+
It does not matter what you leave beyond the returned k (hence they are underscores).
50+
51+
52+
Constraints:
53+
54+
0 <= nums.length <= 100
55+
0 <= nums[i] <= 50
56+
0 <= val <= 100
57+
58+
*
59+
*/
60+
class RemoveElement {
61+
62+
// V0
63+
// IDEA : 2 POINTERS
64+
/**
65+
* //--------------------
66+
* Example 1
67+
* //--------------------
68+
*
69+
* nums = [3,2,2,3], val = 3
70+
*
71+
* [3,2,2,3]
72+
* s
73+
* f
74+
*
75+
* [2,3,2,3] if nums[f] != val, swap, move s
76+
* s s
77+
* f
78+
*
79+
* [2,2,3,3] if nums[f] != val, swap, move s
80+
* s s
81+
* f
82+
*
83+
* [2,2,3,3]
84+
* s
85+
* f
86+
*
87+
*
88+
* //--------------------
89+
* Example 2
90+
* //--------------------
91+
*
92+
* nums = [0,1,2,2,3,0,4,2], val = 2
93+
*
94+
*
95+
* [0,1,2,2,3,0,4,2] if nums[f] != val, swap, move s
96+
* s s
97+
* f
98+
*
99+
* [0,1,2,2,3,0,4,2] if nums[f] != val, swap, move s
100+
* s s
101+
* f
102+
*
103+
* [0,1,2,2,3,0,4,2]
104+
* s
105+
* f
106+
*
107+
* [0,1,2,2,3,0,4,2]
108+
* s
109+
* f
110+
*
111+
* [0,1,3,2,2,0,4,2] if nums[f] != val, swap, move s
112+
* s s
113+
* f
114+
*
115+
* [0,1,3,0,2,2,4,2] if nums[f] != val, swap, move s
116+
* s s
117+
* f
118+
*
119+
* [0,1,3,0,4,2,2,2] if nums[f] != val, swap, move s
120+
* s s
121+
* f
122+
*
123+
* [0,1,3,0,4,2,2,2]
124+
* s
125+
* f
126+
*/
127+
public int removeElement(int[] nums, int val) {
128+
int s = 0;
129+
for (int f = 0; f < nums.length; f++){
130+
if (nums[f] != val){
131+
nums[s] = nums[f];
132+
s += 1;
133+
}
134+
}
135+
return s;
136+
}
137+
138+
// V1
139+
// IDEA : 2 POINTERS
140+
// https://leetcode.com/problems/remove-element/solutions/3670940/best-100-c-java-python-beginner-friendly/
141+
public int removeElement_1(int[] nums, int val) {
142+
int index = 0;
143+
for (int i = 0; i < nums.length; i++) {
144+
if (nums[i] != val) {
145+
nums[index] = nums[i];
146+
index++;
147+
}
148+
}
149+
return index;
150+
}
151+
152+
// V2
153+
// IDEA : 2 POINTERS
154+
// https://leetcode.com/problems/remove-element/solutions/3102906/java-best-solution-o-n-time-complexity/
155+
public int removeElement_2(int[] nums, int val) {
156+
int i = 0;
157+
for (int j = 0; j < nums.length; j++) {
158+
if (nums[j] != val) {
159+
int temp = nums[i];
160+
nums[i] = nums[j];
161+
nums[j] = temp;
162+
i++;
163+
}
164+
}
165+
return i;
166+
}
167+
}

0 commit comments

Comments
 (0)