首页 > 其他 > 详细

[LeetCode] Remove Duplicates from Sorted Array

时间:2015-06-09 23:20:13      阅读:156      评论:0      收藏:0      [点我收藏+]

Well, a medium problem. Use two pointers. One points to the curren number and the other points to the last unique number. Once duplicates occur, move the unique pointer to the right and copy the current number to it.

The code is pretty short.

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

[LeetCode] Remove Duplicates from Sorted Array

原文:http://www.cnblogs.com/jcliBlogger/p/4564607.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!