We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent efb05a9 commit 4a74aacCopy full SHA for 4a74aac
java/0287-find-the-duplicate-number.java
@@ -3,24 +3,26 @@
3
// Space Complexity: O(1)
4
5
class Solution {
6
-
7
public int findDuplicate(int[] nums) {
8
- int fast = nums[0];
9
- int slow = nums[0];
10
- boolean first = true;
11
- while (first || fast != slow) {
12
- if (first) first = false;
+ int slow = 0;
+ int fast = 0;
+
+ do {
13
slow = nums[slow];
14
fast = nums[nums[fast]];
15
- if (fast == slow) break;
16
}
17
- int slow2 = nums[0];
18
- while (slow2 != slow) {
19
20
- slow2 = nums[slow2];
+ while (slow != fast);
+ int slow2 = 0;
21
22
- if (slow2 == slow) return slow;
+ slow2 = nums[slow2];
23
24
- return slow;
+ while (slow != slow2);
25
26
+ return slow2;
27
28
0 commit comments