287. Find the Duplicate number--binary search/slow and fast hands

Source: Internet
Author: User
Tags diff repetition
Given an array nums containing n + 1 integers where each integer is between 1 and N (inclusive), prove that at least one D Uplicate number must exist. Assume that there are only one duplicate number, find the duplicate one.

Note:
You must the Modify the array (assume the array was read only).
You must use only constant, O (1) Extra space.
Your runtime complexity should is less than O (N2).

There is a duplicate number in the array, but it could was repeated more than once.


Solution of 1.O (NLGN):

Thought and before the 378. Kth smallest Element in a Sorted matrix-binary search/heap similar.

The topic requires that we can not change the original array, that is, the original array can not be sorted, and can not use the extra space, then the hash table God horse will not have to consider, and said time is less than O (N2), you can not use brute force method, we can only consider using binary search method, we in the difference [1, n] , first find midpoint mid, then traverse the entire array, count all the number less than equals mid, if the number is greater than mid, then the repetition value between [Mid+1, N], conversely, the repetition value should be between [1, mid-1], and so on, until the search is complete, The low at this point is the duplicate value we requested, see the code below:

int Findduplicate (int* nums, int numssize) {
    int i = 1, j = numsSize-1, Mid, CNT;
    while (I < j) {
        cnt = 0;
        Mid = (i + j)/2;
        for (int k = 0; k < numssize; k++)
            if (Nums[k] <= mid) cnt++;
        if (cnt > Mid) j = Mid; 
        else I = mid + 1;
    }
    return i;
}

Solution of 2.O (n):

The idea is similar to linked list cycle, linked list Cycle 2. This question also has the O (n) solution, but did not understand ... The core idea of the speed of the pointer in the previous topic linked list Cycle II There are applications, where the use of more ingenious. See also: https://discuss.leetcode.com/topic/25913/ My-easy-understood-solution-with-o-n-time-and-o-1-space-without-modifying-the-array-with-clear-explanation.


See this topic may think of a kind of problem encountered before Singlenumber:

1) All elements except one element appear in the array two times to find the element that appears only once.

2) all elements except one element appear in the array three times to find the element that appears only once.

3) All elements in the array appear two times, with two elements appearing only once to find the two elements.

........

Thus, it is possible to think of an XOR method, but there is no way to find it.


3. Lenovo:

Now that you have mentioned the three questions above, let's summarize the ideas of these three questions:

See also: http://www.cnblogs.com/grandyang/p/4741122.html

Http://www.cnblogs.com/grandyang/p/4263927.html

1) All items are different or, the same item is different or the result is 0. Then the remaining value is the value of the item.

public int singlenumber (int[] nums) {
        int res = nums[0];
        for (int i = 1; i < nums.length; i++)
            res = res ^ nums[i];

        return res;
    }

2) This problem is the previous single number of the individual numbers of extension, the solution of the problem is more unique, is the use of the computer to store the characteristics of the digital character to do, this problem is in addition to a separate number, the array of other numbers have appeared three times, then still use bit to operate bit Operation to solve the problem. We can set up a 32-digit number to count each of the 1 occurrences, and we know that if a person is 1, then if the integer appears three times, 3 is 0, we add the corresponding bit of each number to the 3, and the final remaining number is a separate number. The code is as follows:

public static int Singlenumber (int[] nums) {
    int len = nums.length, result = 0;
    for (int i = 0; i <; i++) {
        int sum = 0;
        for (int j = 0; J < Len; J + +) {
            sum + = (Nums[j] >> i) & 1;
        }
        Result |= (sum% 3) << i;
    }
    return result;
    }
3) This problem is the two single number of the first and the individual numbers and single number II separate numbers of the second extension, to tell the truth, this kind of bit operation bit manipulation, if you have not encountered a similar topic, think it is difficult to come out, So I can only search the Internet to find the solution of the great God, it is really ingenious ah. This problem is actually a very clever use of the single number alone the solution, because the solution is accurate to find only one occurrence of the number, but only if the other number must appear two times before the line. And this problem has two numbers have only appeared once, then if we can find a way to divide the original array into two decimal groups, the difference between the two numbers in two decimal groups, so that the single number of separate calls to the solution can be answered. So how to do it, first we first put the original array all different or up, then we will get a number, this number is two different numbers XOR result, we take out any one of them as ' 1 ' bit, for convenience, we use a &=-A to remove the right end as ' 1 ' bit, and then the numbers in the original array, then the two different numbers we asked for are divided into two groups, respectively, two groups of the numbers are different or up, you can get the final result, see the code is as follows:

Class Solution {public
:
    vector<int> singlenumber (vector<int>& nums) {
        int diff = Accumulate (Nums.begin (), Nums.end (), 0, bit_xor<int> ());
        Diff &=-diff;
        vector<int> Res (2, 0);
        for (auto &a:nums) {
            if (A & diff) res[0] ^= A;
            else res[1] ^= A;
        }
        return res;
    }
};



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.