码迷,mamicode.com
首页 > 其他好文 > 详细

[LeetCode] Remove Duplicates from Sorted Array II

时间:2015-06-09 23:10:18      阅读:126      评论:0      收藏:0      [点我收藏+]

标签:

Well, an extension of Remove Duplicates from Sorted Array.

The program is fairly similar to that in this solution.

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

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.

[LeetCode] Remove Duplicates from Sorted Array II

标签:

原文地址:http://www.cnblogs.com/jcliBlogger/p/4564646.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!