leetCode #26 Remove Duplicates from Sorted Array
删除相同数字
1 class Solution { 2 public: 3 int removeDuplicates(vector<int>& nums) { 4 int count = 0; 5 if (nums.size() == 1) 6 { 7 return nums.size() - count; 8 } 9 for (int i = 1; i < nums.size(); ++i) 10 { 11 if (nums[i] == nums[i-1]) 12 { 13 ++count; 14 } 15 else 16 { 17 if (count) 18 { 19 nums[i-count] = nums[i]; 20 } 21 } 22 } 23 return nums.size() - count; 24 } 25 };
限于小鸭酱水平有限,错误的地方或有更好的办法请指出,非常感谢!