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

时间: 2024-12-29 10:13:48

leetcode287的相关文章

LeetCode-287 寻找重复数

传送门:https://leetcode-cn.com/problems/find-the-duplicate-number/submissions/给定一个包含 n + 1 个整数的数组 nums,其数字都在 1 到 n 之间(包括 1 和 n),可知至少存在一个重复的整数.假设只有一个重复的整数,找出这个重复的数. 示例 1: 输入: [1,3,4,2,2]输出: 2示例 2: 输入: [3,1,3,4,2]输出: 3说明: 不能更改原数组(假设数组是只读的).只能使用额外的 O(1) 的空

leetcode287 Find the Duplicate Number

1 """ 2 Given an array nums containing n + 1 integers where each integer is between 1 and n (inclusive), prove that at least one duplicate number must exist. Assume that there is only one duplicate number, find the duplicate one. 3 Example

LeetCode 287

Find the Duplicate Number Given an array nums containing n + 1 integers where each integer is between 1 and n (inclusive), prove that at least one duplicate number must exist. Assume that there is only one duplicate number, find the duplicate one. No