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 }
  • 相关阅读:
    listView控件演示程序
    猪悟能淘宝商品下载专家v3版开发手记
    c# WebClient类
    分隔控件splitter演示程序
    C#中SESSIONID的获取
    工具栏toolBar演示程序
    Cookie总结
    C#获取网页源码并且自动判断网页字符集编码
    如何在Google Code上建立私有代码版本库
    iOS应用程序生命周期(前后台切换,应用的各种状态)详解
  • 原文地址:https://www.cnblogs.com/Juntaran/p/5511700.html
Copyright © 2011-2022 走看看