Find the Duplicate Number 解答

Question

Given an array nums containing n + 1 integers where each integer is between 1 and n (inclusive), prove that at least one duplicate number must exist. Assume that there is only one duplicate number, find the duplicate one.

Note:

  1. You must not modify the array (assume the array is read only).
  2. You must use only constant, O(1) extra space.
  3. Your runtime complexity should be less than O(n2).
  4. There is only one duplicate number in the array, but it could be repeated more than once.

Solution 1 -- Binary Search

题目要求时间复杂度小于O(n2),于是我们就想有没有O(n log n)或者O(n)的做法。一些排序算法是O(n log n),但是题目要求不能更改原序列且空间复杂度为O(1)。

Binary search的复杂度是O(log n),前提是排好序的数组。所以肯定不能用输入数组来进行二分查找。

这一题提供了一个思路是对可行解序列/集合进行二分查找。

由于题目中有明确的各个元素的取值范围,我们可以判断出解一定在[1, n]这个区间内。start = 1, end = n。对于每个mid值,我们计算等于mid的count和小于等于mid的count。

注意:

smallerCount 是和mid值比较。比如mid = 5,那么如果smallerCount <= 5,说明解一定不在[1,5]这个区间内。

 1 public class Solution {
 2     public int findDuplicate(int[] nums) {
 3         int start = 1, end = nums.length - 1, mid;
 4         while (start + 1 < end) {
 5             mid = (end - start) / 2 + start;
 6             int smallerMid = 0;
 7             for (int i : nums) {
 8                 if (i <= mid) {
 9                     smallerMid++;
10                 }
11             }
12             // Compare with mid
13             if (smallerMid <= mid) {
14                 start = mid;
15             } else{
16                 end = mid;
17             }
18         }
19         int countStart = 0;
20         for (int i : nums) {
21             if (i == start) {
22                 countStart++;
23             }
24         }
25         if (countStart > 1) {
26             return start;
27         }
28         return end;
29     }
30 }

Solution 2

参考Discuss,发现有O(n)的解法

参考Linked List II,我们将输入的array也可看作是list,每个数组元素代表这个node的next

 1 public class Solution {
 2     public int findDuplicate(int[] nums) {
 3         if (nums == null || nums.length < 2) {
 4             return 0;
 5         }
 6         int slow = nums[0];
 7         int fast = nums[slow];
 8         while (fast != slow) {
 9             slow = nums[slow];
10             fast = nums[nums[fast]];
11         }
12         fast = 0;
13         while (fast != slow) {
14             slow = nums[slow];
15             fast = nums[fast];
16         }
17         return slow;
18     }
19 }
时间: 2024-10-26 16:15:36

Find the Duplicate Number 解答的相关文章

【LeetCode从零单刷】Find the Duplicate Number

题目: Given an array nums containing n + 1 integers where each integer is between 1 and n (inclusive), prove that at least one duplicate number must exist. Assume that there is only one duplicate number, find the duplicate one. Note: You must not modif

Find the Duplicate Number

 Given an array nums containing n + 1 integers where each integer is between 1 and n (inclusive), prove that at least one duplicate number must exist. Assume that there is only one duplicate number, find the duplicate one. Note: You must not modify

leetcode Find the Duplicate Number

题目连接 https://leetcode.com/problems/find-the-duplicate-number/ Find the Duplicate Number Description Given an array nums containing n + 1 integers where each integer is between 1 and n (inclusive), prove that at least one duplicate number must exist.

[LeetCode] 287. Find the Duplicate Number(Floyd判圈算法)

传送门 Description Given an array nums containing n + 1 integers where each integer is between 1 and n (inclusive), prove that at least one duplicate number must exist. Assume that there is only one duplicate number, find the duplicate one. Note: You mu

LeetCode 287. Find the Duplicate Number

Find the Duplicate Number | LeetCode OJhttps://leetcode.com/problems/find-the-duplicate-number/ 这个题目属于编码比较简单但解法分析过程比较复杂. 首先,把1~n放入0~n个元素,必定有两个或以上元素重复.数字里没有0,所以从下标0出发不会再回到最初的元素0. 假设当前的下标为x,下一步的下标为f(x),若f(x) = A[x], 也即每次跳到一个元素,则下一步移动到当前元素值对应的元素下标.我们来证明

&lt;LeetCode OJ&gt; 287. Find the Duplicate Number

287. Find the Duplicate Number My Submissions Question Total Accepted: 18097 Total Submissions: 48596 Difficulty: Hard Given an array nums containing n + 1 integers where each integer is between 1 and n (inclusive), prove that at least one duplicate

287. Find the Duplicate Number *HARD*

Given an array nums containing n + 1 integers where each integer is between 1 and n (inclusive), prove that at least one duplicate number must exist. Assume that there is only one duplicate number, find the duplicate one. Note: You must not modify th

LeetCode-Cycle Detection,Find the Duplicate Number

      本来以为这题很简单,但是看要求,第一不能改变这个数组,第二只能使用O(1)的空间,第三时间复杂度小于O(n^2),就不能使用遍历数组的方式来解决了. 有两种方法,一种是利用Binary Search,一种是利用Floyd的cycle detection算法. Binary Search Method This method is based on a theroy called Pigeonhole princinpal. In mathematics,Pigeonhole prin

[LeetCode] Find the Duplicate Number 寻找重复数

Given an array nums containing n + 1 integers where each integer is between 1 and n (inclusive), prove that at least one duplicate element must exist. Assume that there is only one duplicate number, find the duplicate one. Note: You must not modify t