本文主要是介绍LeetCode *** 80. Remove Duplicates from Sorted Array II,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
题目:
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();}
};
这篇关于LeetCode *** 80. Remove Duplicates from Sorted Array II的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!