您的位置:首页 > 其它

[LeetCode] Remove Duplicates from Sorted Array II

2015-06-09 21:39 489 查看
Well, an extension of Remove Duplicates from Sorted Array.

The program is fairly similar to that in this solution.

int removeDuplicates(vector<int>& nums) {
if (nums.size() <= 2) return nums.size();
int pos = 1;
for (int i = 2; i < nums.size(); i++)
if (nums[i] != nums[pos] || nums[i] != nums[pos - 1])
nums[++pos] = nums[i];
return pos + 1;
}


After playing with it for a while, you may notice that the code can be easily extended to accommodate 3, 4, ... duplicates by modifying lines 3, 4, 5 in a similar manner.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: