Given a sorted array, remove the duplicates in-place such that each element appear only once and return the new length.
Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extra memory.
Example:
Given nums = [1,1,2], Your function should return length = 2, with the first two elements of nums being 1 and 2 respectively. It doesn't matter what you leave beyond the new length.
这道题目跟之前有道题,Remove Element,非常像:
1 class Solution { 2 public: 3 int removeDuplicates(vector<int>& nums) { 4 int count = 0; 5 int n = nums.size(); 6 for (int i = 1; i < n; ++i) 7 { 8 if (nums[i] == nums[i - 1]) 9 { 10 ++count;//记重复元素的个数 11 } 12 else 13 { 14 nums[i - count] = nums[i]; 15 } 16 } 17 18 return n - count; 19 } 20 };