You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
24 lines
411 B
24 lines
411 B
2 years ago
|
package class34;
|
||
|
|
||
|
public class Problem_0287_FindTheDuplicateNumber {
|
||
|
|
||
|
public static int findDuplicate(int[] nums) {
|
||
|
if (nums == null || nums.length < 2) {
|
||
|
return -1;
|
||
|
}
|
||
|
int slow = nums[0];
|
||
|
int fast = nums[nums[0]];
|
||
|
while (slow != fast) {
|
||
|
slow = nums[slow];
|
||
|
fast = nums[nums[fast]];
|
||
|
}
|
||
|
fast = 0;
|
||
|
while (slow != fast) {
|
||
|
fast = nums[fast];
|
||
|
slow = nums[slow];
|
||
|
}
|
||
|
return slow;
|
||
|
}
|
||
|
|
||
|
}
|