Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
// https://leetcode-cn.com/problems/remove-duplicates-from-sorted-array/solution/
#include <iostream>
#include <vector>
using namespace std;
class Solution
{
public:
int removeDuplicates(vector<int>& nums)
{
if(nums.size() == 0) return 0;
int i=0;
for (int j = 1; j < nums.size(); j++)
{
if (nums[j] != nums[i])
{
i++;
nums[i] = nums[j];
}
}
return i+1;
}
};
int main(int argc, char const *argv[])
{
Solution s;
vector<int> nums;
nums.push_back(1);
nums.push_back(1);
nums.push_back(2);
nums.push_back(2);
nums.push_back(3);
printf("%d\n",s.removeDuplicates(nums));
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.