题目:
Follow up for "Remove Duplicates":
What if duplicates are allowed at most twice?
For example,
Given sorted array nums = [1,1,1,2,2,3],
Your function should return length = 5, with the first five elements of nums being 1, 1, 2, 2 and 3. It doesn't matter what you leave beyond the new length.
分析:
啥?
代码:
class Solution {
public:
int removeDuplicates(vector<int>& nums) {
if(nums.size()==0)return 0;
vector<int>::iterator it1=nums.begin(),it2=nums.begin();
while(it2!=nums.end()-1){
if(*it2!=*(it2+1)){
it2++;
it1=it2;
}else{
if(it1==it2)it2++;
else it2=nums.erase(it2);
}
}
return nums.size();
}
};
本文探讨了一种算法解决方案,该方案允许在一个已排序的整数数组中最多保留每个元素两次,并返回处理后的数组长度。例如,对于输入数组[1,1,1,2,2,3],经过处理后应返回长度为5的数组,包含元素1,1,2,2,3。
790

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



