#include<iostream>
#include<vector>
using namespace std;
class Solution
{
public:
int removeDuplicates(vector<int>&nums)
{
if (nums.empty())
return 0;
int index = 0;
for (int i = 1; i < nums.size(); i++)
{
if (nums[index] != nums[i])
nums[++index] = nums[i];
}
return index + 1;
}
};
int main()
{
vector<int>nums = { 1, 1, 2, 3, 4, 4, 5, 6, 7 };
Solution c;
cout << "数组的长度:" << c.removeDuplicates(nums)<< endl;
system("pause");
return 0;
#include<vector>
using namespace std;
class Solution
{
public:
int removeDuplicates(vector<int>&nums)
{
if (nums.empty())
return 0;
int index = 0;
for (int i = 1; i < nums.size(); i++)
{
if (nums[index] != nums[i])
nums[++index] = nums[i];
}
return index + 1;
}
};
int main()
{
vector<int>nums = { 1, 1, 2, 3, 4, 4, 5, 6, 7 };
Solution c;
cout << "数组的长度:" << c.removeDuplicates(nums)<< endl;
system("pause");
return 0;
}
运行结果: