找出数组中重复的数字
来源:互联网 发布:cad软件锁不起作用 编辑:程序博客网 时间:2024/05/01 02:47
实现算法1
- 修改了原始数组
- 空间复杂度为O(1)
bool duplicate(int numbers[],int length){ if(numbers==nullptr||length<=0){ return false; } for (int i=0; i<length; ++i) { if(numbers[i]<0||numbers[i]>length-1){ return false; } } for (int i=0; i<length; ++i) { while (numbers[i]!=i) { if (numbers[i]==numbers[numbers[i]]) { cout<<numbers[i]<<endl;//重复的数字 return true; } int temp=numbers[i]; numbers[i]=numbers[temp]; numbers[temp]=temp; } } return false;}
实现算法2
int getDuplication(const int* numbers,int length){ if(numbers==nullptr||length<=0){ return -1; } int start=1; int end=length-1; while (end>=start) { int middle=((end-start)>>1)+start; int count=countRange(numbers,length,start,middle); if(end==start){ if(count>1){ return start; }else{ break; } } if (count>(middle-start+1)) { end=middle; }else{ start=middle+1; } } return -1;}int countRange(const int* numbers,int length,int start,int end){ if(numbers==nullptr){ return 0; } int count=0; for (int i=0; i<length; i++) { if(numbers[i]>=start&&numbers[i]<=end){ ++count; } } return count;}
阅读全文
0 0
- 找出数组中重复的数字
- 找出数组中重复的数字
- 找出数组中重复的数字
- 找出数组中重复的数字
- 剑指offer:找出数组中重复的数字
- Java实现找出数组中重复的数字
- 面试题:从1001个整数元素的数组中找出重复的数字
- 《牛客网剑指offer50题》找出数组中任意一个重复的数字
- 剑指offer——面试题3:找出数组中重复的数字
- 找出数组中唯一的重复元素
- 找出数组中重复的数
- 找出数组中唯一重复的数
- 找出数组中唯一的重复元素
- 找出数组中唯一的重复元素
- 找出数组中唯一的重复元素
- 找出数组中唯一的重复元素
- 找出数组中唯一的重复元素
- 找出数组中重复的数
- RecyclerView 滚动到指定position,并置顶
- H(n) UVA
- 玫瑰花
- 我的电路实践
- ELK之安装与部署
- 找出数组中重复的数字
- 4.1.2Groovy理解闭包
- jdk环境配置
- 软件工程(C编码实践篇)学习总结
- android获取系统标准时区的时间
- VBA基础打卡
- 简单的桃心,haha
- JavaScript的this学习笔记
- 11.13学习心得