lintcode100 删除排序数组中的重复数字

删除排序数组中的重复数字 

 

给定一个排序数组,在原数组中删除重复出现的数字,使得每个元素只出现一次,并且返回新的数组的长度。

不要使用额外的数组空间,必须在原地没有额外空间的条件下完成。

样例

给出数组A =[1,1,2],你的函数应该返回长度2,此时A=[1,2]

 1 class Solution {
 2 public:
 3     /*
 4      * @param nums: An ineger array
 5      * @return: An integer
 6      */
 7     int removeDuplicates(vector<int> &nums) {
 8         // write your code here
 9         int len = nums.size();
10         if (nums.empty()) return 0;
11         
12         int index = 1;
13         for (int i = 1; i < len; ++i) {
14             if (nums[i] != nums[i - 1]) {
15                 nums[index] = nums[i];
16                 index++;
17             }
18         }
19         return index;
20     }
21 };
原文地址:https://www.cnblogs.com/gousheng/p/7644478.html