feat: 双指针
This commit is contained in:
26
80.remove-duplicates-from-sorted-array-ii.java
Normal file
26
80.remove-duplicates-from-sorted-array-ii.java
Normal file
@@ -0,0 +1,26 @@
|
||||
/*
|
||||
* @lc app=leetcode id=80 lang=java
|
||||
*
|
||||
* [80] Remove Duplicates from Sorted Array II
|
||||
*/
|
||||
|
||||
// @lc code=start
|
||||
class Solution {
|
||||
public int removeDuplicates(int[] nums) {
|
||||
int i = -1, freq = 0;
|
||||
for(int j = 0; j< nums.length; j++) {
|
||||
if(i<0 || nums[i] == nums[j] && freq < 2) {
|
||||
i++;
|
||||
nums[i] = nums[j];
|
||||
freq++;
|
||||
} else if(nums[i] != nums[j]) {
|
||||
freq = 1;
|
||||
i++;
|
||||
nums[i] = nums[j];
|
||||
}
|
||||
}
|
||||
return i+1;
|
||||
}
|
||||
}
|
||||
// @lc code=end
|
||||
|
||||
Reference in New Issue
Block a user