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.

Example

Example 1:

Input:
[1,3,4,2,2]
Output:
 2

Example 2:

Input:
 [3,1,3,4,2]

Output:
 3

Note

是Set Mismatch的变种,不能改变数组

使用龟兔赛跑的快慢法吧,实在想不到。。

do {
tortoise = nums[tortoise];
hare = nums[nums[hare]];
} while (tortoise != hare);

找入口,一个指向数组起点,一个指向乌龟指针,一起走直到相遇

Code

Last updated