删除排序数组中的重复项(C++实现)
代码:
#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 << "返回删除数组重复项后数组的长度:" << '\n';
std::cout <<index+1 << '\n';
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 << "返回删除数组重复项后数组的长度:" << '\n';
std::cout <<index+1 << '\n';
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 << "新数组:" << '\n';
for(int i=0;i<tem;i++){
std::cout << "新数组:" << '\n';
for(int i=0;i<tem;i++){
std::cout << nums[i] << ' ';
}
return 0;
}
}
return 0;
}
测试结果: