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.

 /*************************************************************************
> File Name: LeetCode287.c
> Author: Juntaran
> Mail: JuntaranMail@gmail.com
> Created Time: Tue 17 May 2016 20:20:12 PM CST
************************************************************************/ /************************************************************************* 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. ************************************************************************/ #include <stdio.h> int findDuplicate(int* nums, int numsSize)
{
if(numsSize > )
{
int slow = nums[];
int fast = nums[nums[]]; while(slow != fast) //check the existence of the loop;
{
printf("%d %d\n", slow, fast);
slow = nums[slow];
fast = nums[nums[fast]];
}
printf("%d %d\n", slow, fast); fast = ;
while(slow != fast) //find the start of the loop which means at least two integer are the same value;
{
slow = nums[slow];
fast = nums[fast];
}
return slow;
}
return -;
} int main()
{
int nums[] = {,,,,};
int numsSize = ; int ret = findDuplicate( nums, numsSize);
printf("%d\n", ret);
}

LeetCode 287的更多相关文章

  1. LeetCode 287. Find the Duplicate Number (python 判断环,时间复杂度O(n))

    LeetCode 287. Find the Duplicate Number 暴力解法 时间 O(nlog(n)),空间O(n),按题目中Note"只用O(1)的空间",照理是过 ...

  2. [LeetCode] 287. Find the Duplicate Number 寻找重复数

    Given an array nums containing n + 1 integers where each integer is between 1 and n (inclusive), pro ...

  3. LeetCode 287. Find the Duplicate Number (找到重复的数字)

    Given an array nums containing n + 1 integers where each integer is between 1 and n (inclusive), pro ...

  4. LeetCode | 287. 寻找重复数

    特别感谢LeetCode大佬陈牧远的科普知识 给定一个包含 n + 1 个整数的数组 nums,其数字都在 1 到 n 之间(包括 1 和 n),可知至少存在一个重复的整数.假设只有一个重复的整数,找 ...

  5. Java实现 LeetCode 287 寻找重复数

    287. 寻找重复数 给定一个包含 n + 1 个整数的数组 nums,其数字都在 1 到 n 之间(包括 1 和 n),可知至少存在一个重复的整数.假设只有一个重复的整数,找出这个重复的数. 示例 ...

  6. [LeetCode]287. 寻找重复数(二分)

    题目 给定一个包含 n + 1 个整数的数组 nums,其数字都在 1 到 n 之间(包括 1 和 n),可知至少存在一个重复的整数.假设只有一个重复的整数,找出这个重复的数. 示例 1: 输入: [ ...

  7. LeetCode : 287. Find the Duplicate Number

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAACRAAAAMMCAYAAAAhQhmZAAAMFGlDQ1BJQ0MgUHJvZmlsZQAASImVlw ...

  8. [LeetCode] 287. Find the Duplicate Number 解题思路

    Given an array nums containing n + 1 integers where each integer is between 1 and n (inclusive), pro ...

  9. leetcode 287寻找重复数

    这道题用STL容器就很好写了,可以用set也可以用map, 用unordered_map的C++代码如下: class Solution { public: int findDuplicate(vec ...

随机推荐

  1. 《dive into python3》 笔记摘录

    1.list can hold  arbitrary  objects and can expand dynamically as new items are added. A list is an  ...

  2. (剑指Offer)面试题33:把数组排成最小的数

    题目: 输入一个正整数数组,把数组里所有数字拼接起来排成一个数,打印能拼接出的所有数字中最小的一个.例如输入数组{3,32,321},则打印出这三个数字能排成的最小数字为321323. 思路: 1.全 ...

  3. ref和out的使用与区别

    out的使用 ————————————————————————————————————————————————— class Program    {        static void Main( ...

  4. Android 通过xml 自定义图片

    <?xml version="1.0" encoding="utf-8"?><shape xmlns:android="http:/ ...

  5. 【官方文档】Hadoop分布式文件系统:架构和设计

    http://hadoop.apache.org/docs/r1.0.4/cn/hdfs_design.html 引言 前提和设计目标 硬件错误 流式数据访问 大规模数据集 简单的一致性模型 “移动计 ...

  6. How do I place a group of functions or variables in a specific section?

    http://supp.iar.com/Support/?Note=27498 EWARM v5.xx (and newer) The placement of a few functions in ...

  7. WatchKit App Submission Issues

    查看原文: http://leancodingnow.com/watchkit-app-submission-issues/ I submitted a new version of my app P ...

  8. C#利用lambda在函数中创建内部函数

    有使用过JS的朋友,相信都知道function.JS中的function是可以在里面在定义一个作为内部使用的.有时为了控制作用域,或者这种小函数只在这个函数体内会使用,所以就不希望在外部在作额外的定义 ...

  9. D2 前端技术论坛总结(下)

    此篇文章不接上篇了,下午4场我就不一一介绍了,主要总结下 D2 整场下来都讲了些什么.   整场下来,就3个关键词:nodejs,多终端,工程化   nodejs 从杭js到d2,大会上提到最多的词汇 ...

  10. ThinkPHP中pathinfo模式与URL重写

    Thinkphp中的pathinfo模式 http://serverName/appName/module/action/id/1/ pathinfo模式 在不考虑路由的情况下,第一个参数会被解析成模 ...