码迷,mamicode.com
首页 > 其他好文 > 详细

287. Find the Duplicate Number

时间:2018-10-07 13:55:07      阅读:145      评论:0      收藏:0      [点我收藏+]

标签:poi   problems   code   number   while   fas   tco   solution   turn   

 

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 }

 

287. Find the Duplicate Number

标签:poi   problems   code   number   while   fas   tco   solution   turn   

原文地址:https://www.cnblogs.com/goPanama/p/9749863.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!