Find the Duplicate Number
Example
Input:
[1,3,4,2,2]
Output:
2Input:
[3,1,3,4,2]
Output:
3Note
do {
tortoise = nums[tortoise];
hare = nums[nums[hare]];
} while (tortoise != hare);Code
Last updated
Input:
[1,3,4,2,2]
Output:
2Input:
[3,1,3,4,2]
Output:
3do {
tortoise = nums[tortoise];
hare = nums[nums[hare]];
} while (tortoise != hare);Last updated
class Solution {
public int findDuplicate(int[] nums) {
// Find the intersection point of the two runners.
int tortoise = nums[0];
int hare = nums[0];
do {
tortoise = nums[tortoise];
hare = nums[nums[hare]];
} while (tortoise != hare);
// Find the "entrance" to the cycle.
int ptr1 = nums[0];
int ptr2 = tortoise;
while (ptr1 != ptr2) {
ptr1 = nums[ptr1];
ptr2 = nums[ptr2];
}
return ptr1;
}
}