287. Find the Duplicate Number

 

https://leetcode.com/problems/linked-list-cycle-ii/discuss/44777/Concise-JAVA-solution-based-on-slow-fast-pointers

fast slow, 刚开始全部初始化为0,当作起始点

 

 1 class Solution {
 2     public int findDuplicate(int[] nums) {
 3         if(nums.length <= 1) return -1;
 4         int slow = nums[0];
 5         int fast = nums[nums[0]];
 6         while(slow != fast){
 7             slow = nums[slow];
 8             fast = nums[nums[fast]];
 9         }
10         fast = 0;
11         while(fast != slow){
12             fast = nums[fast];
13             slow = nums[slow];
14         }
15         return fast;
16         
17     }
18 }

 

posted @ 2018-10-07 12:49  jasoncool1  阅读(110)  评论(0编辑  收藏  举报