Skip to content

Commit 9cf5f5c

Browse files
committed
feat: solved 287
1 parent 237bb2c commit 9cf5f5c

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.fghpdf.FindTheDuplicateNumber;
2+
3+
/**
4+
* @author fghpdf
5+
* @date 2019/12/23
6+
* https://leetcode.com/problems/find-the-duplicate-number/
7+
* fast and slow
8+
* When fast==slow, they meet at the entry point of the circle
9+
**/
10+
public class Solution {
11+
public int findDuplicate3(int[] nums) {
12+
if (nums.length < 1) {
13+
return -1;
14+
}
15+
16+
int slow = nums[0];
17+
int fast = nums[slow];
18+
while (slow != fast) {
19+
slow = nums[slow];
20+
fast = nums[nums[fast]];
21+
}
22+
23+
fast = 0;
24+
while (slow != fast) {
25+
fast = nums[fast];
26+
slow = nums[slow];
27+
}
28+
29+
return slow;
30+
}
31+
}

0 commit comments

Comments
 (0)