核心思想:两个游标
/*
* @lc app=leetcode id=80 lang=cpp
*
* [80] Remove Duplicates from Sorted Array II
*/
// @lc code=start
class Solution {
public:
int removeDuplicates(vector<int>& nums) {
int N = nums.size();
if(N<=0) return 0;
int L = 1;
int R = 1;
int cnt = 1;
int now = nums[0];
while(R<N){
if(nums[R] == now){
if(cnt >= 2){
R++;
continue;
}
cnt++;
}else{
now = nums[R];
cnt = 1;
}
nums[L++] = nums[R++];
}
return L;
}
};
// @lc code=end
本文介绍了解决LeetCode上编号为80的问题Remove Duplicates from Sorted Array II的方法。通过使用两个游标的技术,有效地移除了已排序数组中多余的重复元素,确保每个元素最多出现两次。
5585

被折叠的 条评论
为什么被折叠?



