LeetCode ---- Find the Duplicate Number

Source: Internet
Author: User

LeetCode ---- Find the Duplicate Number

Find the Duplicate Number

 

Given an array nums containing n + 1 integers where each integer is between 1 and n (random SIVE), prove that at least one duplicate number must exist. assume that there is only one duplicate number, find the duplicate one.

Note:

  1. You must not modify the array (assume the array is read only ).
  2. You must use only constant, O (1) extra space.
  3. Your runtime complexity shocould be lessO(n2).
  4. There is only one duplicate number in the array, but it cocould be repeated more than once.

    Analysis:

    Given an array, the length of the array is n + 1, and the number is between 1 and n. In this way, there must be one number in the array twice. Assume that only one number is repeated to find the number.

    Requirement: the array cannot be modified and is read-only. Only constant spaces are allowed. The algorithm complexity is smaller than O (n ^ 2), meaning no violence. Assume that there is only one repeat number, but it can appear many times.

    I thought about this question for a long time. View Discuss to understand.

    Solution 1:

    I used the fresh Cycle detection method. I remember I wrote a question called Linked List Cycle yesterday, which was written by this method.

    The idea is to map array elements and subscripts one by one based on the characteristics of array elements. For example, if the array is 13422, 0 corresponds to 1, 1, 3, 2, 4, and so on. In this way, if you think of this ing as a chain, you can follow the subscript, subscript corresponding value, subscript corresponding value corresponding to the underlying value, always traverse, there is a ring in the chain, in the end, it will be in a loop. For example, for the above array, it will eventually fall into a loop where subscript 2 corresponds to value 4, subscript 4 corresponds to value 2, and subscript 2 corresponds to value 4.

    It is not enough to find the loop. We also need to find the number into the loop, which is the number of duplicates to find. You can use a new value with a subscript of 0 to map from the beginning. When we encounter a pointer in the previous loop, we find the starting point of the loop.

     

    Code 1:

     

    Class Solution (object): def findDuplicate (self, nums): type nums: List [int]: rtype: int slow, fast = 0, 0 while True: slow = nums [slow] fast = nums [nums [fast] if slow = fast: break # fast and slow must have met at a certain point in the loop, however, the encounter point is not necessarily the element that just enters the loop. # You need to start from 0 and continue searching for find = 0 while find! = Slow: slow = nums [slow] find = nums [find] return find


     

    Solution 2:

    Use the binary method.

     

    Code:

     

    class Solution(object):    def findDuplicate(self, nums):                :type nums: List[int]        :rtype: int                l, r = 0, len(nums) - 1        while l <= r:            mid = l + (r - l) / 2            cnt = 0            for n in nums:                if n <= mid:                    cnt += 1            if cnt > mid:                r = mid - 1            else:                l = mid + 1        return l

     

     

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.