-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFind_the_Duplicate_Number.cpp
More file actions
52 lines (52 loc) · 1.39 KB
/
Find_the_Duplicate_Number.cpp
File metadata and controls
52 lines (52 loc) · 1.39 KB
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
48
49
50
51
52
//Find the Duplicate Number,https://leetcode.com/problems/find-the-duplicate-number/
//抽屉原理,整个数组中如果小于等于n/2的数的数量大于n/2,说明1到n/2这个区间是肯定有重复数字的
//否则,在[n/2+1,n]的范围中存在重复数
int findDuplicate(vector<int> nums)
{
int min = 0, max = nums.size() - 1;
while (min <= max)
{
// 找到中间那个数
int mid = min + (max - min) / 2;
int cnt = 0;
// 计算总数组中有多少个数小于等于中间数
for (int i = 0; i < nums.size(); i++)
{
if (nums[i] <= mid)
{
cnt++;
}
}
// 如果小于等于中间数的数量大于中间数,说明前半部分必有重复
if (cnt > mid)
{
max = mid - 1;
// 否则后半部分必有重复
}
else
{
min = mid + 1;
}
}
return min;
}
//映射找环法,快慢指针的方法
//https://leetcode.com/discuss/61514/understood-solution-space-without-modifying-explanation
int findDuplicate_v2(vector<int> nums)
{
int slow = 0;
int fast = 0;
do
{
slow = nums[slow];
fast = nums[nums[fast]];
}
while (slow != fast);
int find = 0;
while (find != slow)
{
slow = nums[slow];
find = nums[find];
}
return find;
}