zoukankan      html  css  js  c++  java
  • LeetCode 287

    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 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 /*************************************************************************
     2     > File Name: LeetCode287.c
     3     > Author: Juntaran
     4     > Mail: JuntaranMail@gmail.com
     5     > Created Time: Tue 17 May 2016 20:20:12 PM CST
     6  ************************************************************************/
     7 
     8 /*************************************************************************
     9     
    10     Find the Duplicate Number
    11     
    12     Given an array nums containing n + 1 integers where 
    13     each integer is between 1 and n (inclusive), 
    14     prove that at least one duplicate number must exist. 
    15     Assume that there is only one duplicate number, find the duplicate one.
    16 
    17     Note:
    18     You must not modify the array (assume the array is read only).
    19     You must use only constant, O(1) extra space.
    20     Your runtime complexity should be less than O(n2).
    21     There is only one duplicate number in the array, 
    22     but it could be repeated more than once.
    23 
    24  ************************************************************************/
    25 
    26 #include <stdio.h>
    27 
    28 int findDuplicate(int* nums, int numsSize)
    29 {
    30     if(numsSize > 0)
    31     {
    32         int slow = nums[0];
    33         int fast = nums[nums[0]];
    34         
    35         while(slow != fast) //check the existence of the loop;
    36         {
    37             printf("%d %d
    ", slow, fast);
    38             slow = nums[slow];
    39             fast = nums[nums[fast]];
    40         }
    41         printf("%d %d
    ", slow, fast);
    42         
    43         fast = 0;
    44         while(slow != fast) //find the start of the loop which means at least two integer are the same value;
    45         {
    46             slow = nums[slow];
    47             fast = nums[fast];
    48         }
    49         return slow;
    50     }
    51     return -1;
    52 }
    53 
    54 int main()
    55 {
    56     int nums[] = {1,3,4,2,2};
    57     int numsSize = 5;
    58     
    59     int ret = findDuplicate( nums, numsSize);
    60     printf("%d
    ", ret);
    61 }
  • 相关阅读:
    i春秋CTF-web-upload
    [转]SEP 11.x 迁移
    通过SEP禁用USB
    通过SEP屏蔽共享文件夹
    SEP图示
    离线更新SEPM服务器的病毒定义库
    SEPM安装完之后的一些细节之处
    Bloomberg SEP 12.x 迁移小记
    LiveUpdate Adminstrator配置手册
    Symantec更新服务器
  • 原文地址:https://www.cnblogs.com/Juntaran/p/5511700.html
Copyright © 2011-2022 走看看