Description:
https://leetcode.com/problems/remove-duplicates-from-sorted-array/#/description
Algorithm:
Use swap.
Code:
class Solution
{
public:
int removeDuplicates(vector<int>& nums)
{
if (nums.size() == 0) return 0;
int length = 0;
for (int i = 0; i < nums.size(); i++) {
if (nums[length] != nums[i])
nums[++length] = nums[i];
}
return length + 1;
}
};
Timing:
O(n)
Mine is 35ms (beats 35%). Actually I believe my code is the same with the code of 22ms, but I don’t know why I’m slower. 🙁