Created
April 2, 2026 17:21
-
-
Save thinkphp/0af5b1beb117cbe3ab491afc7884f365 to your computer and use it in GitHub Desktop.
floyd-cycle-detection.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //Floyd's Algorithms Cycle detection - find duplicate Complexity O(n) | |
| class Solution { | |
| public int findDuplicate(int[] nums) { | |
| int slow = nums[0]; | |
| int fast = nums[0]; | |
| do { | |
| slow = nums[slow]; | |
| fast = nums[ nums[ fast ] ] | |
| } while( slow != fast)l | |
| fast = nums[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