转自https://www.cnblogs.com/grandyang/p/4329128.html
空间复杂度为O(1),即不创建新的数组,在原数组上进行操作。
考虑两个flag:_cur,_next。_cur=0;_next=1;
if nums[_cur]==nums[_next]--->_next++;
else:nums[++_cur]=nums[_next++];
class Solution {
public:
int removeDuplicates(vector<int>& nums) {
if (nums.empty()) return 0;
int pre = 0, cur = 0, n = nums.size();
while (cur < n) {
if (nums[pre] == nums[cur]) ++cur;
else nums[++pre] = nums[cur++];
}
return pre + 1;
}
};