删除数组中重复元素 (使用stl::set)
2014-09-03 16:05
411 查看
/*
*程序作用删除数中重复的元素,先使用set 遍历一次数组,然后在使用两个指针,以及set查重,
*去重复之后使用0填补多余空间
*复杂度 O(NlogN)
*空间复杂度 O(N)
*/
#include<iostream>
#include<set>
using namespace std;
void delete_over_arry(int *a,int len);
void print(int *a ,int len);
int main()
{
int p[]={1,1,2,5,3,7,3,4,8,2,1,3,9,1};
print(p,sizeof(p)/sizeof(int));
delete_over_arry( p,sizeof(p)/sizeof(int));
print(p,sizeof(p)/sizeof(int));
return 0;
}
void print(int *a ,int len)
{
for(int i=0;i<len;i++)
cout<<a[i];
cout<<endl;
}
void delete_over_arry(int *a,int len)
{
set<int> temp_set;
int *set_p=a+1;
int *new_p=a+1;
int count=0;
temp_set.insert(a[0]);
for(int i=1;i<len;i++)
{
if(temp_set.count(*set_p))
{
set_p++;
count++;
}
else
{
temp_set.insert(*set_p);
*new_p++=*set_p++;
}
}
for(int i=0;i<count;i++)
*new_p++=0;
}以下是程序运行结果:
[trageday@lei-yum code_test]$ g++ -o delete_overarray delete_overarray.cpp
[trageday@lei-yum code_test]$ ./delete_overarray
11253734821391
12537489000000
*程序作用删除数中重复的元素,先使用set 遍历一次数组,然后在使用两个指针,以及set查重,
*去重复之后使用0填补多余空间
*复杂度 O(NlogN)
*空间复杂度 O(N)
*/
#include<iostream>
#include<set>
using namespace std;
void delete_over_arry(int *a,int len);
void print(int *a ,int len);
int main()
{
int p[]={1,1,2,5,3,7,3,4,8,2,1,3,9,1};
print(p,sizeof(p)/sizeof(int));
delete_over_arry( p,sizeof(p)/sizeof(int));
print(p,sizeof(p)/sizeof(int));
return 0;
}
void print(int *a ,int len)
{
for(int i=0;i<len;i++)
cout<<a[i];
cout<<endl;
}
void delete_over_arry(int *a,int len)
{
set<int> temp_set;
int *set_p=a+1;
int *new_p=a+1;
int count=0;
temp_set.insert(a[0]);
for(int i=1;i<len;i++)
{
if(temp_set.count(*set_p))
{
set_p++;
count++;
}
else
{
temp_set.insert(*set_p);
*new_p++=*set_p++;
}
}
for(int i=0;i<count;i++)
*new_p++=0;
}以下是程序运行结果:
[trageday@lei-yum code_test]$ g++ -o delete_overarray delete_overarray.cpp
[trageday@lei-yum code_test]$ ./delete_overarray
11253734821391
12537489000000
相关文章推荐
- C++使用STL::vector删除数组内的重复元素
- Java 使用List,Set将Person数组中的重复元素删除
- 删除数组中重复元素-Hash表方法和set方法
- 删除数组中重复元素--哈希表方法和set方法
- 使用ES6的Set去除数组的重复元素
- 【C++ STL应用与实现】56: 使用std::unique删除重复元素
- 整形数组合并(STL vector删除重复元素)
- Java中删除数组中重复元素
- 删除数组中的重复元素
- STL—vector删除重复元素
- Javascript数组删除重复元素的方法
- 使用javascript去掉数组中重复元素的方法
- 删除有序数组中的重复元素
- PHP删除重复数组元素
- 删除数组中重复的元素(C#实现)
- 删除重复数组中重复元素
- php array_flip() 删除数组重复元素
- VB中删除数组重复元素
- 删除数组中存在重复的元素。
- 用javascript如何删除一个数组中的重复元素