Description Link to heading
26.remove-duplicates-from-sorted-array
Solution Link to heading
We use two pointers fast
and slow
, fast
used to check if duplicated, slow
use for assignment.
Code Link to heading
#include <vector>
using std::vector;
class Solution {
public:
int removeDuplicates(vector<int> &nums) {
int fast = 1;
int slow = 0;
for (slow = 0; slow < nums.size(); slow++) {
while (fast < nums.size() && nums[fast] == nums[fast - 1])
fast++;
if (fast == nums.size())
break;
nums[slow + 1] = nums[fast++];
}
return slow + 1;
}
};