Skip to content

Commit

Permalink
Update 0287-find-the-duplicate-number.java
Browse files Browse the repository at this point in the history
Used a do while loop to make the code more concise.
  • Loading branch information
ColstonBod-oy authored Apr 24, 2023
1 parent efb05a9 commit 4a74aac
Showing 1 changed file with 15 additions and 13 deletions.
28 changes: 15 additions & 13 deletions java/0287-find-the-duplicate-number.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,26 @@
// Space Complexity: O(1)

class Solution {

public int findDuplicate(int[] nums) {
int fast = nums[0];
int slow = nums[0];
boolean first = true;
while (first || fast != slow) {
if (first) first = false;
int slow = 0;
int fast = 0;

do {
slow = nums[slow];
fast = nums[nums[fast]];
if (fast == slow) break;
}
int slow2 = nums[0];
while (slow2 != slow) {
if (first) first = false;
slow2 = nums[slow2];

while (slow != fast);

int slow2 = 0;

do {
slow = nums[slow];
if (slow2 == slow) return slow;
slow2 = nums[slow2];
}
return slow;

while (slow != slow2);

return slow2;
}
}

0 comments on commit 4a74aac

Please sign in to comment.