Skip to content

Instantly share code, notes, and snippets.

@Siyu-Lei
Created May 26, 2018 05:04
Show Gist options
  • Save Siyu-Lei/da3ac299888a3ed19583dce35fc2280c to your computer and use it in GitHub Desktop.
Save Siyu-Lei/da3ac299888a3ed19583dce35fc2280c to your computer and use it in GitHub Desktop.
class Solution {
public int findDuplicate(int[] nums) {
int slow = nums[0];
int fast = nums[nums[0]];
while (slow != fast) {
slow = nums[slow];
fast = nums[nums[fast]];
}
fast = 0;
while (slow != fast) {
slow = nums[slow];
fast = nums[fast];
}
return fast;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment