public int findDuplicate(int[] nums) {
// Find the intersection point of the two runners.
int slow = 0;
int fast = 0;
do {
slow = nums[slow];
fast = nums[nums[fast]];
} while (slow != fast);
// Find the "entrance" to the cycle.
int find = 0;
while (find != slow) {
find = nums[find];
slow = nums[slow];
}
return find;
}
网友评论