Remove Duplicates from Sorted Array
来源:互联网 发布:imovie windows 编辑:程序博客网 时间:2024/06/11 16:38
- 单词:
duplicate相同的数
Sort有序
Array数组 - 思路:
1.Allocate extra space for another array
#include <iostream>#include <vector>using namespace std;class SortedArray{public: void Print(vector<int> &nums) { for(vector<int>::iterator it=nums.begin();it!=nums.end();it++) cout<<*it<<" "; } int RemoveDuplicates(vector<int> &src,vector<int> &dst) { if(src.empty()) return 0; int index=0; dst.push_back(src[index]); for(int i=1;i<src.size();i++) { if(src[index]!=src[i]){ index++; src[index]=src[i]; dst.push_back(src[index]); } } return index+1; }};int main(){ //创建两个vector容器 vector<int> src; vector<int> dst; //vector容器初始化 src.push_back(1); src.push_back(1); src.push_back(1); src.push_back(1); src.push_back(2); src.push_back(2); src.push_back(2); src.push_back(2); src.push_back(2); //创建类 SortedArray sortedArray; //实现 int cont=sortedArray.RemoveDuplicates(src,dst); cout<<cont<<endl; sortedArray.Print(dst); return 0;}
2. Not allocate extra space for another array
#include <iostream>#include <vector>#include <algorithm>using namespace std;class A{public: int Solution(int *a,int len) { if(len<0) return 0; int index=0; a[index]=a[0]; for(int i=1;i<len;i++) { if(a[index]!=a[i]) { index++; a[index]=a[i]; } } return index+1; }};int main(){ int a[10]={1,1,1,1,2,2,2,3,3,3}; A b; int len=b.Solution(a,10); for(int i=0;i<len;i++) cout<<a[i]<<" "; return 0;}
0 0
- Remove Duplicates from Sorted Array
- Remove Duplicates from Sorted Array
- Remove Duplicates from Sorted Array
- Remove Duplicates from Sorted Array
- Remove Duplicates from Sorted Array
- Remove Duplicates from Sorted Array
- Remove Duplicates From Sorted Array
- Remove Duplicates from Sorted Array
- Remove Duplicates from Sorted Array
- Remove Duplicates from Sorted Array
- Remove Duplicates from Sorted Array
- Remove Duplicates from Sorted Array
- Remove Duplicates from Sorted Array
- Remove Duplicates from Sorted Array
- Remove Duplicates from Sorted Array
- Remove Duplicates from Sorted Array
- Remove Duplicates from Sorted Array
- Remove Duplicates from Sorted Array
- 设计模式之建造者模式
- POJ 3038 How Many Answers Are Wrong(加权并查集)
- UIView、UIWindow和CALayer的理解
- Windows Internals
- Memcache Brief Intro
- Remove Duplicates from Sorted Array
- Android动画效果translate、scale、alpha、rotate详解
- 历届试题 错误票据
- Win7下IIS 7的安装配置与文件发布
- 一种合适的智能优化算法
- JQuery实现DIV其他动画效果
- JavaScript之数据类型(转)
- 步骤比较细致的 圣杯布局说明
- 160. Intersection of Two Linked Lists