Skip to content

Commit

Permalink
add 080 java
Browse files Browse the repository at this point in the history
  • Loading branch information
yennanliu committed May 27, 2024
1 parent 9c29d2a commit fed14f1
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@
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)
073 | [Set Matrix Zeroes](https://leetcode.com/problems/set-matrix-zeroes/) | [Python](./leetcode_python/Array/set-matrix-zeroes.py), [Java](./leetcode_java/src/main/java/LeetCodeJava/Array/SetMatrixZeroes.java) | _O(m * n)_ | _O(1)_ | Medium |Curated Top 75, `matrix`, `amazon`| OK* (4)
080 | [Remove Duplicates from Sorted Array II](https://leetcode.com/problems/remove-duplicates-from-sorted-array-ii/)| [Python](./leetcode_python/Array/remove-duplicates-from-sorted-array-ii.py) | _O(n)_ | _O(1)_ | Medium |`two pointers`,AGAIN, `basic`, `trick`, `fb`, `#26 Remove Duplicates from Sorted Array`| AGAIN************ (8)
080 | [Remove Duplicates from Sorted Array II](https://leetcode.com/problems/remove-duplicates-from-sorted-array-ii/)| [Python](./leetcode_python/Array/remove-duplicates-from-sorted-array-ii.py), [Java](./leetcode_java/src/main/java/LeetCodeJava/Array/RemoveDuplicatesFromSortedArray2.java) | _O(n)_ | _O(1)_ | Medium |`two pointers`,AGAIN, `basic`, `trick`, `fb`, `#26 Remove Duplicates from Sorted Array`| AGAIN************ (8)
118 | [Pascal's Triangle](https://leetcode.com/problems/pascals-triangle/)| [Python](./leetcode_python/Array/pascals-triangle.py) | _O(n^2)_ | _O(1)_ | Easy |`trick`, `basic`| AGAIN** (2)
119 | [Pascal's Triangle II](https://leetcode.com/problems/pascals-triangle-ii/)| [Python](./leetcode_python/Array/pascals-triangle-ii.py) | _O(n^2)_ | _O(1)_ | Easy |array, check with `# 118 Pascal's Triangle`, `amazon`| OK** (4)
121 | [Best Time to Buy and Sell Stock](https://leetcode.com/problems/best-time-to-buy-and-sell-stock/)| [Python](./leetcode_python/Array/best-time-to-buy-and-sell-stock.py), [Java](./leetcode_java/src/main/java/LeetCodeJava/Array/BestTimeToBuyAndSellStock.java) | _O(n)_ | _O(1)_ | Easy |Curated Top 75, `dp`,`basic`,`greedy`,`UBER`, `M$`, `amazon`, `fb`| OK****** (7) (but again)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package LeetCodeJava.Array;

// https://leetcode.com/problems/remove-duplicates-from-sorted-array-ii/description/

public class RemoveDuplicatesFromSortedArray2 {

// V0
// IDEA : 2 POINTERS ( gpt)
// https://github.com/yennanliu/CS_basics/blob/master/leetcode_python/Array/remove-duplicates-from-sorted-array-ii.py
public int removeDuplicates(int[] nums) {
if (nums.length < 3) {
return nums.length;
}

int slow = 1; // slow starts from 1
for (int fast = 2; fast < nums.length; fast++) { // fast starts from 2
// If the condition is met, update the array and increment slow pointer
if (nums[slow] != nums[fast] || nums[slow] != nums[slow - 1]) {
nums[++slow] = nums[fast];
}
}
return slow + 1; // Return the length of the modified array
}

// V1
// IDEA : 2 POINTERS
// The approach employs a two-pointer strategy. The variable j is used to keep track of the current position in the modified array where elements are being stored without violating the constraint. The loop iterates through the array, and for each element, it checks whether it is the same as the element two positions behind the current j. If it is, it means there are already two occurrences of this element in the modified array, and we should skip adding another one to adhere to the constraint. Otherwise, the element is added to the modified array at position j, and j is incremented.
// https://leetcode.com/problems/remove-duplicates-from-sorted-array-ii/solutions/4804983/beats-100-0ms-advanced-two-pointer-approach-java-c-python-rust/
public int removeDuplicates_1(int[] nums) {
int j = 1;
for (int i = 1; i < nums.length; i++) {
if (j == 1 || nums[i] != nums[j - 2]) {
nums[j++] = nums[i];
}
}
return j;
}

// V2
// IDEA : 2 POINTERS
// https://leetcode.com/problems/remove-duplicates-from-sorted-array-ii/solutions/4511964/easy-o-n-python-java-go-c-beginner-friendly/
public int removeDuplicates_2(int[] nums) {

int index = 1;
int occurance = 1;

for(int i=1; i < nums.length; i++){
if (nums[i] == nums[i-1]){
occurance++;
}else{
occurance = 1;
}

if (occurance <= 2){
nums[index] = nums[i];
index++;
}
}
return index;
}

}

0 comments on commit fed14f1

Please sign in to comment.