国产xxxx99真实实拍_久久不雅视频_高清韩国a级特黄毛片_嗯老师别我我受不了了小说

資訊專欄INFORMATION COLUMN

287. Find the Duplicate Number

fevin / 1911人閱讀

題目:
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.

Note:
You must not modify the array (assume the array is read only).
You must use only constant, O(1) extra space.
Your runtime complexity should be less than O(n2).
There is only one duplicate number in the array, but it could be repeated more than once.

解答:
1.Binary Tree

public int findDuplicate(int[] nums) {
    int start = 1, end = nums.length - 1;
    while (start + 1 < end) {
        int mid = start + (end - start) / 2;
        int left = 0, right = 0;
        for (int a : nums) {
            if (a <= mid) left++;
        }
       
        if (left <= mid) start = mid;
        else end = mid;
    }
    int countStart = 0, countEnd = 0;
    for (int i = 0; i < nums.length; i++) {
        if (nums[i] == start) countStart++;
        if (nums[i] == end) countEnd++;
    }
    return countStart > countEnd ? start : end;
}

2.LinkedList Find the starting point of circle

public int findDuplicate(int[] nums) {
    int n = nums.length;
    
    int slow = nums[0];
    int fast = nums[nums[0]];
    while (slow != fast) {
        slow = nums[slow];
        fast = nums[nums[fast]];
    }
    
    fast = 0;
    while (slow != fast) {
        slow = nums[slow];
        fast = nums[fast];
    }
    return slow;
}

文章版權歸作者所有,未經允許請勿轉載,若此文章存在違規行為,您可以聯系管理員刪除。

轉載請注明本文地址:http://specialneedsforspecialkids.com/yun/65012.html

相關文章

  • LeetCode[287] Find the Duplicate Number

    摘要:復雜度思路每次通過二分法找到一個值之后,搜索整個數組,觀察小于等于這個數的個數。考慮,小于這個位置的數的個數應該是小于等于這個位置的。要做的就是像找中的環一樣,考慮重復的點在哪里。考慮用快慢指針。代碼把一個指針放回到開頭的地方 LeetCode[287] Find the Duplicate Number Given an array nums containing n + 1 in...

    canopus4u 評論0 收藏0
  • [LeetCode] 287. Find the Duplicate Number

    Problem 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 nu...

    rickchen 評論0 收藏0
  • 287. Find the Duplicate Number

    摘要:如果的數字都只有一個,那么我們會形成一個閉合的環。如果有重復數字出現的話,如下這里就有一個的環,就是重復數字如果到每個數字都只出現一次,在里的個數應該就是個數大于的話,前面的數字里就會有重復。 // 如果[1,n]的數字都只有一個,那么我們會形成一個閉合的環。 idx 1 2 3 4 val 3 1 4 2 1->3->4->2 這樣就是一個閉合的環。 如果有重復數字出現的話,如下:...

    galaxy_robot 評論0 收藏0
  • [Leetcode] Find the Duplicate Number 找到重復數字

    摘要:暴力法復雜度時間空間思路如果不用空間的話,最直接的方法就是選擇一個數,然后再遍歷整個數組看是否有跟這個數相同的數就行了。二分法復雜度時間空間思路實際上,我們可以根據抽屜原理簡化剛才的暴力法。 Find the Duplicate Number Given an array nums containing n + 1 integers where each integer is bet...

    chnmagnus 評論0 收藏0
  • [LeetCode] Find the Duplicate Number

    Problem 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 nu...

    MoAir 評論0 收藏0

發表評論

0條評論

最新活動
閱讀需要支付1元查看
<