[JAVA] Cycle / T : O(N), S : O(1)


  • 0
    J
    class Solution {
        public int findDuplicate(int[] nums) {
            int p1 = nums[0];
            int p2 = nums[nums[0]];
            
            while(p1 != p2){
                p1 = nums[p1];
                p2 = nums[nums[p2]];
            }
            
            p1 = 0;
            while(p1 != p2){
                p1 = nums[p1];
                p2 = nums[p2];
            }
            
            return p1;
        }
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.