-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfindDuplicate.h
47 lines (43 loc) · 1.3 KB
/
findDuplicate.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
//
// Created by so_go on 2019/6/14.
//
#ifndef SRC_FINDDUPLICATE_H
#define SRC_FINDDUPLICATE_H
#include<vector>
using namespace std;
class FindDuplicate {
public:
int findDuplicate(vector<int>& nums) {
// 选择[m, n)中的一个中间数字l,若[m, l)中的数字数目大于 l - m ,则重复数字必在[m, l)中,反之,则重复数字在[l, n)
// nums.size() == n + 1, last = n + 1
return binarySearch(nums, 1, nums.size());
}
int binarySearch(vector<int> &nums, int m, int n){
/*备选区间[m, n)
* 截止条件: 当 n -m = 1, 返回m
* 选择中间数字 l = m + n /2, 统计[m, l)中的数字数目count,
* 当count > l - m 则, 在[m, l)中继续搜索
* 否则,搜索[l, n)
*/
// cout << m << ' ' << n << endl;
if( n - m == 1){
return m;
}
int l = (m + n) / 2;
int res = -1;
int count = 0;
for(auto ptr = nums.begin(); ptr != nums.end(); ptr++){
if( *ptr >= m and *ptr < l){
count++;
}
}
if(count > l - m){
res = binarySearch(nums, m, l);
}
else{
res = binarySearch(nums, l, n);
}
return res;
}
};
#endif //SRC_FINDDUPLICATE_H