-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
63 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
leetcode_java/src/main/java/LeetCodeJava/Array/RemoveDuplicatesFromSortedArray2.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |