找出数组{1,2,3,4,...N-1}中出现的唯一重复数
2016-01-17 10:15
309 查看
异或法:
2.位图法
上面的都有额外的开销
1.Hash法
2.循环链表法
不能从元素0开始,防止死循环!!
/* 找出数组{1,2,3,4,...N-1}中出现的唯一重复数 */ #include <iostream> /* run this program using the console pauser or add your own getch, system("pause") or input loop */ using namespace std; int xor_findDup(int* a,int count) { int i = 0; int temp = 0; for(i=0;i<count;i++) { temp = temp ^ a[i]; } for(i=1;i<count;i++) { temp = temp ^ i; } return temp; } int main(int argc, char *argv[]) { int a[]={1,3,2,4,2}; int length = sizeof(a)/sizeof(int); cout << "Duplication is : " << xor_findDup(a,length) << endl; return 0; }
2.位图法
/* 找出数组{1,2,3,4,...N-1}中出现的唯一重复数 */ #include <iostream> #include <stdlib.h> /* run this program using the console pauser or add your own getch, system("pause") or input loop */ using namespace std; int xor_findDup(int* a,int count) { int i = 0; int temp = 0; int* tempArray = (int*) malloc(sizeof(int)*(count)); for(i=0;i<count;i++) { tempArray[i] = 0; } for(i=0;i<count;i++) { if(tempArray[a[i]]==0) { tempArray[a[i]] = 1; } else { temp = a[i]; return a[i]; } } return temp; } int main(int argc, char *argv[]) { int a[]={1,3,1,4,2}; int length = sizeof(a)/sizeof(int); cout << "Duplication is : " << xor_findDup(a,length) << endl; return 0; }
上面的都有额外的开销
1.Hash法
/* 找出数组{1,2,3,4,...N-1}中出现的唯一重复数 */ #include <iostream> #include <stdlib.h> /* run this program using the console pauser or add your own getch, system("pause") or input loop */ using namespace std; int xor_findDup(int* a,int count) { int i = 0; for(i=0;i<count;i++) { if(a[i]>0) { if(a[a[i]]>0) { a[a[i]] = - a[a[i]]; } else { return a[i]; } } else { if(a[-a[i]] > 0) { a[-a[i]] = -a[-a[i]]; } else { return -a[i]; } } } // return 0; } int main(int argc, char *argv[]) { int a[]={4,1,2,1,3}; int length = sizeof(a)/sizeof(int); cout << "Duplication is : " << xor_findDup(a,length) << endl; return 0; }
2.循环链表法
/* 找出数组{1,2,3,4,...N-1}中出现的唯一重复数 */ #include <iostream> #include <stdlib.h> /* run this program using the console pauser or add your own getch, system("pause") or input loop */ using namespace std; int xor_findDup(int* a,int count) { int i = 0; int x = 0,y = 0; do { x = a[a[x]]; //一次走两步 y = a[y]; //一次走一步 cout << x << "->" << a[x] << "->"<<a[a[x]] << " ; " << y << "-" << a[y] << endl; }while(x != y); //找到环中的一个点 cout << endl; cout << x << endl; do { x = a[x]; //一次走一步 y = a[y]; //一次走一步 cout << x << "->" << a[x] << " ; " << y << "->" << a[y] << endl; } while(x != y); //找到入口点 return x; // return 0; } int main(int argc, char *argv[]) { int a[]={4,1,2,3,4}; int length = sizeof(a)/sizeof(int); cout << "Duplication is : " << xor_findDup(a,length) << endl; return 0; }
不能从元素0开始,防止死循环!!
相关文章推荐
- android控件edittext-addTextChangedListener监听文本的变化
- php 引用和global
- VB.net操控網頁整理加實戰篇
- linux的远程唤醒
- Hello,World!
- 设计模式学习01—单例模式
- 清结算概念
- ajax页面动态加载弹出窗口
- 3450: Tyvj1952 Easy|概率与期望
- spring mvc 接受多对象的处置
- I NEED A OFFER!---hdu1203(01背包)
- 智能指针的原理及实现
- Codeforces 599 C Day at the Beach
- R7-2015-26: Advantech EKI Dropbear Authentication Bypass (CVE-2015-7938)
- 马化腾最崇拜的作家吴晓波《历代经济变革得失》读书笔记
- Codevs_P1835 [SDOI2010]魔法猪学院(A*K短路+堆)
- 排序(选择,冒泡,二分法)、进制转换
- 链接
- 浅谈CSS外边距重叠
- java web 学习十五(jsp基础语法)