[Leetcode] Find the Duplicate number

Source: Internet
Author: User

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 element must exist. Assume that there are only one duplicate number, find the duplicate one.

Note:

    1. You must the Modify the array (assume the array was read only).
    2. You must use only constant extra space.
    3. Your runtime complexity should is less than O(n2) .

Two-point lookup, Complexity O (NLOGN).

1 classSolution {2  Public:3     intFindduplicate (vector<int>&nums) {4         intL =0, r = nums.size ()-1, M;5          while(L <=r) {6m = L + ((r-l) >>1);7             intCNT =0;8              for(Auto A:nums)if(a <= m) + +CNT;9             if(CNT <= m) L = m +1;Ten             Elser = M-1; One         } A         returnl; -     } -};

[Leetcode] Find the Duplicate number

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.