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 }
  • 相关阅读:
    Codeforces Round #632 (Div. 2)
    Educational Codeforces Round 83 E. Array Shrinking
    Codeforces Round #626 D. Present
    I
    java学习-get和post请求
    java学习-MD5消息摘要算法
    分销系统数据库设计
    java获得当前日期是今年的第几周,以及这周的开始日期的方法
    分销系统的用户关系,用户与推广链接的数据库设计。设计思路
    git工具,conflict冲突解决方法
  • 原文地址:https://www.cnblogs.com/Juntaran/p/5511700.html
Copyright © 2011-2022 走看看