20 lines
395 B
C#
20 lines
395 B
C#
|
public class Solution {
|
||
|
public int FindDuplicate(int[] nums) {
|
||
|
int slow = nums[0];
|
||
|
int fast = nums[0];
|
||
|
|
||
|
do {
|
||
|
slow = nums[slow];
|
||
|
fast = nums[nums[fast]];
|
||
|
} while (fast != slow);
|
||
|
|
||
|
slow = nums[0];
|
||
|
while (slow != fast) {
|
||
|
slow = nums[slow];
|
||
|
fast = nums[fast];
|
||
|
}
|
||
|
|
||
|
return slow;
|
||
|
}
|
||
|
}
|