Leetcode Note: Find the Duplicate Number

Source: Internet
Author: User

Leetcode Note: Find the Duplicate Number

I. Description

Given an array nums containingn + 1Integers where each integer is1Andn(Aggressive), prove that at least one duplicate element 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 extra space.
Your runtime complexity shocould be lessO(n^2).
There is only one duplicate number in the array, but it cocould be repeated more than once.

Ii. Question Analysis

The general idea is to given + 1An array of integers. The size of each integer is[1, n]To prove that at least one duplicate element exists. At the same time, assume that only one number in the array is repeated to find the repeated number.

Note:

The array cannot be modified (assuming the array is read-only). The running time complexity of the constant space must be less O(n^2)Only one repeat number exists in the array, but the number may be repeated multiple times.

There are many limitations on the subject. At first we couldn't think of the optimal method. Here we provide a novel method: The ing method for finding the ring. In fact, this method comes from the chain table for finding the ring. Time Complexity:O(n), Space complexityO(1).

First, assume that there are no duplicates in an array, you can add the subscript of the array to its corresponding element (1 ~ n. For example[4, 1, 3, 2], The ing relationship is0 -> 4, 1 -> 1, 2 -> 3,3 -> 2.

This ing can represent a function.f(n), WherenIs subscript,f(n)Is the number mapped. If we start from the subscript 0, calculate a value based on this function, use this value as the new subscript, use this function for calculation, and so on, we can generate a sequence similar to a linked list.

At this time, if the array itself has repeated elements, there will be many-to-one ing, such as the Array[2, 1, 3, 1], The ing relationship is:0 -> 2, {1, 3} -> 1, 2 -> 3. Friends who are familiar with the linked list know that this sequence shows a loop! Consistent with the linked list. If you traverse this ing:0 -> 2 -> 3 -> 1 -> 1 -> 1 -> ...Where, the starting point of the ring is the number of duplicates required by the question.

Therefore, this question can be converted to the starting point of the loop. The implementation method is similar to that of Linked List Cycle II. Similarly, the usage speed (fast,slow) Two subscripts, from0At the beginning, the fast subscript is mapped twice each cycle, that is:fast = num[num[fast]]The slow subscript is mapped once every cycle, that is:slow = num[slow]Untilfast == slowThis phase is equivalent to the process in which the fast pointer and the slow pointer encounter when the linked list finds a ring. This conclusion proves that the linked list has a ring. In this case, it can prove that there are repeated elements in the array!

The second step is to find out the duplicate element, that is, to find the entry of the ring. In this case, another variable is required.finderSubscripts0Departure, andfastThe operations are the same, and each cycle is mapped twice:fast = num[num[fast]], At the same time,slowMaintain a ing. When the two subscripts meet,slowIt is the starting point of the ring, that is, the number of duplicates.

Iii. Sample Code

class Solution {public:    int findDuplicate(vector
  
   & nums) {        int slow = 0;        int fast = 0;        int finder = 0;        while (true)        {            slow = nums[slow];            fast = nums[nums[fast]];            if (slow == fast)                break;        }        while (true)        {            slow = nums[slow];            finder = nums[finder];            if (slow == finder)                return slow;        }    }};
  

Iv. Summary

The idea of this question is quite novel and difficult to come up with. In addition, there is a solution that uses the binary method, but it is not the best solution.

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.