cs: add «287. Find the Duplicate Number»
Signed-off-by: Matej Focko <me@mfocko.xyz>
This commit is contained in:
parent
54e3ac8c5a
commit
a56a328d6b
1 changed files with 19 additions and 0 deletions
19
cs/find-the-duplicate-number.cs
Normal file
19
cs/find-the-duplicate-number.cs
Normal file
|
@ -0,0 +1,19 @@
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue