Created
November 7, 2018 00:42
-
-
Save qiaoxu123/7f9a22b13baca022df12b1a2823ba3bc to your computer and use it in GitHub Desktop.
给定一个排序数组,你需要在原地删除重复出现的元素,使得每个元素只出现一次,返回移除后数组的新长度。 不要使用额外的数组空间,你必须在原地修改输入数组并在使用 O(1) 额外空间的条件下完成。 示例 1: 给定数组 nums = [1,1,2], 函数应该返回新的长度 2, 并且原数组 nums 的前两个元素被修改为 1, 2。 你不需要考虑数组中超出新长度后面的元素。
示例 2: 给定 nums = [0,0,1,1,1,2,2,3,3,4], 函数应该返回新的长度 5, 并且
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution { | |
public: | |
int removeDuplicates(vector<int>& nums) { | |
if(nums.empty()) | |
return 0; | |
int len = 0; | |
for(int i = 1;i < nums.size();++i){ | |
if(nums[i] != nums[len]) | |
nums[++len] = nums[i]; | |
} | |
return len + 1; | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment