首页 > 代码库 > leetcode287

leetcode287

public class Solution {
    public int FindDuplicate(int[] nums) {
        if (nums.Count() > 1)
            {
                int slow = nums[0];
                int fast = nums[nums[0]];
                while (slow != fast)
                {
                    slow = nums[slow];
                    fast = nums[nums[fast]];
                }

                fast = 0;
                while (fast != slow)
                {
                    fast = nums[fast];
                    slow = nums[slow];
                }
                return slow;
            }
            return -1;
    }
}

https://leetcode.com/problems/find-the-duplicate-number/#/description

leetcode287