标签:
这个题之前做过,实在没想出什么好方法,今天突然发现这个完全就是个有环链表找开始进入环的题目,真是相当精巧
public class Solution { public int findDuplicate(int[] nums) { int slow = 0; int fast = 0; do { slow = nums[slow]; fast = nums[nums[fast]]; } while (slow != fast); slow = 0; while (slow != fast) { slow = nums[slow]; fast = nums[fast]; } return slow; } }
[LeetCode]Find the Duplicate Number
标签:
原文地址:http://www.cnblogs.com/vision-love-programming/p/5021539.html