数组重复数字 不修改数组
2018-03-11 22:15
148 查看
#include <iostream> #include <cstdlib> #include <stdio.h> //数组长度n+1,内容为1到n的数字,有重复 //二分法 数组中重复数字 不能修改数组内容 时间复杂度O(nlogn) 空间复杂度O(1) int count(int* numbers,int length,int start,int end); int getDuplication(int* numbers, int length){ if(length <= 0 || numbers == nullptr) return -1; //i make mistake here for using return false as return 0; int start=1,end=length; int i=0; while(end>=start){ int middle=start+((end - start) >> 1); //i make mistake here with +1 int range = count(numbers,length,start,middle); if(end == start){ if(range){ return start; } else return -1; } if(range){ end = middle; } else start = middle +1; } } int count(int* numbers,int length,int start,int end){ if(numbers == nullptr || end < start) return false; int countrange=0; for(int i=0;i<length;i++){ if(numbers[i]>=start && numbers[i]<=end){ countrange++; } } if(countrange > (end-start+1)) return 1; else return 0; }
相关文章推荐
- 算法面试题之不修改数组找出重复的数字
- 面试题(四)不修改数组找出重复的数字
- 不修改数组找出重复的数字
- 算法题:不修改数组找出重复的数字
- 不修改数组找出重复的数字
- 剑指第三题:数组重复数字 不修改数组
- 数组中重复的数字
- 数组中重复的数字
- 数组中重复的数字
- 寻找数组中的第K大的元素&找数组中重复数字
- 剑指Offer面试题51:数组中的重复数字
- Leetcode:136.Single Number 找到数组里面的非重复数字,其他数字也最多重复两次
- java 一个int数组 长度为100 随机生成100个数 即1-100 将其插入进数组 插入的数字不能重复
- 数组中重复的数字
- 删除数组中的重复数字I
- lintcode-删除排序数组中的重复数字II-101
- 删除排序数组中的重复数字 II
- LintCode 删除排序数组中的重复数字
- 计蒜客-排序后的数组删除重复数字
- 数组中重复的数字