寻找重复数(medium难度)
https://leetcode-cn.com/problems/find-the-duplicate-number/
<方法一>:二分查找
本方法代码和思路来源:
作者:liweiwei1419
链接:https://leetcode-cn.com/problems/find-the-duplicate-number/solution/er-fen-fa-si-lu-ji-dai-ma-python-by-liweiwei1419/
来源:力扣(LeetCode)
参考代码1:
public class Solution {
public int findDuplicate(int[] nums) {
int len = nums.length;
int left = 1;
int right = len - 1;
while (left < right) {
// 在 Java 里可以这么用,当 left + right 溢出的时候,无符号右移保证结果依然正确
int mid = (left + right) >>> 1;
int cnt = 0;
for (int num : nums) {
if (num <= mid) {
cnt += 1;
}
}
// 根据抽屉原理,小于等于 4 的个数如果严格大于 4 个
// 此时重复元素一定出现在 [1, 4] 区间里
if (cnt > mid) {
// 重复元素位于区间 [left, mid]
right = mid;
} else {
// if 分析正确了以后,else 搜索的区间就是 if 的反面
// [mid + 1, right]
left = mid + 1;
}
}
return left;
}
}
作者:liweiwei1419
链接:https://leetcode-cn.com/problems/find-the-duplicate-number/solution/er-fen-fa-si-lu-ji-dai-ma-python-by-liweiwei1419/
来源:力扣(LeetCode)
参考代码2:
public class Solution {
public int findDuplicate(int[] nums) {
int len = nums.length;
int left = 1;
int right = len - 1;
while (left < right) {
// 在 Java 里可以这么用,当 left + right 溢出的时候,无符号右移保证结果依然正确
//注意这里是left + right + 1;进行上取整,从而保证不会出现死循环。
int mid = (left + right + 1) >>> 1;
int cnt = 0;
for (int num : nums) {
if (num < mid) {
cnt += 1;
}
}
// 根据抽屉原理,严格小于 4 的数的个数如果大于等于 4 个,
// 此时重复元素一定出现在 [1, 3] 区间里
if (cnt >= mid) {
// 重复的元素一定出现在 [left, mid - 1] 区间里
right = mid - 1;
} else {
// if 分析正确了以后,else 搜索的区间就是 if 的反面
// [mid, right]
// 注意:此时需要调整中位数的取法为上取整
left = mid;
}
}
return left;
}
}
作者:liweiwei1419
链接:https://leetcode-cn.com/problems/find-the-duplicate-number/solution/er-fen-fa-si-lu-ji-dai-ma-python-by-liweiwei1419/
来源:力扣(LeetCode)
在采用这种做法时要注意一下细节:
//注意这里是left + right + 1;进行上取整,从而保证不会出现死循环。
int mid = (left + right + 1) >>> 1;
详细解释可以看1095.山脉数组中查找目标值的官方题解视频第9分20秒左右。
<方法二>:快慢指针
本方法思路及代码来源:
作者:kirsche
链接:https://leetcode-cn.com/problems/find-the-duplicate-number/solution/287xun-zhao-zhong-fu-shu-by-kirsche/
来源:力扣(LeetCode)
class Solution {
public int findDuplicate(int[] nums) {
int slow = 0;
int fast = 0;
slow = nums[slow];
fast = nums[nums[fast]];
while(slow != fast){
slow = nums[slow];
fast = nums[nums[fast]];
}
int pre1 = 0;
int pre2 = slow;
while(pre1 != pre2){
pre1 = nums[pre1];
pre2 = nums[pre2];
}
return pre1;
}
}
作者:kirsche
链接:https://leetcode-cn.com/problems/find-the-duplicate-number/solution/287xun-zhao-zhong-fu-shu-by-kirsche/
来源:力扣(LeetCode)