Skip to content

Instantly share code, notes, and snippets.

@2hanX
Created November 22, 2020 07:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 2hanX/1a4f27a9181a8c46f29918b50c7a99a6 to your computer and use it in GitHub Desktop.
Save 2hanX/1a4f27a9181a8c46f29918b50c7a99a6 to your computer and use it in GitHub Desktop.
// 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