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 }
  • 相关阅读:
    JSON排序
    iOS常用控件尺寸大集合
    Android 平台下Ftp 使用模拟器需要注意的问题
    PhoneGap Html5 App 横竖屏切换自动退出
    加减密 DES
    入学测试题
    黑马程序员——JAVA基础之反射
    黑马程序员——JAVA基础之网络编程
    黑马程序员——JAVA基础之正则表达式,网络爬虫
    黑马程序员——JAVA基础之GUI
  • 原文地址:https://www.cnblogs.com/Juntaran/p/5511700.html
Copyright © 2011-2022 走看看