代码:
#include <iostream>
#include <vector>
using std::cout;
using std::endl;
using std::vector;
#include <vector>
using std::cout;
using std::endl;
using std::vector;
class Solution {
public:
int removeDuplicates(vector<int> &nums) {
public:
int removeDuplicates(vector<int> &nums) {
if(nums.empty()){
return 0;
}
int index = 0;
for(int j=1;j<nums.size();j++){
if(nums[index]!=nums[j]){
index++;
nums[index]=nums[j];
}
}
std::cout << "返回删除数组重复项后数组的长度:" << ' ';
std::cout <<index+1 << ' ';
return index+1;
}
};
return 0;
}
int index = 0;
for(int j=1;j<nums.size();j++){
if(nums[index]!=nums[j]){
index++;
nums[index]=nums[j];
}
}
std::cout << "返回删除数组重复项后数组的长度:" << ' ';
std::cout <<index+1 << ' ';
return index+1;
}
};
int main(){
Solution S;
vector<int> nums={1,2,3,3,3,3,4,5};
Solution S;
vector<int> nums={1,2,3,3,3,3,4,5};
int tem = S.removeDuplicates(nums);
std::cout << "新数组:" << ' ';
for(int i=0;i<tem;i++){
std::cout << "新数组:" << ' ';
for(int i=0;i<tem;i++){
std::cout << nums[i] << ' ';
}
return 0;
}
}
return 0;
}
测试结果:
![](https://img2020.cnblogs.com/blog/1765425/202008/1765425-20200804012056759-926574392.png)