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. 使用多个Worker的时候Odoo的系统日志配置

    当我们开启Wokrer来启动Odoo的时候,用默认的日志会出现日志丢失的问题,这个是logger的问题:多个进程对单个文件写入日志.有一个简单的解决办法:配置openerp-server.conf,开 ...

  2. user is not in the sudoers file.This incident will be reported

    我用普通用户ssk登陆,想让ssk成为拥有超级用户的权限的普通用户 开始提示输入密码错误 ,然后就这样了   解决方法如下: 1>.进入超级用户模式.也就是输入"su -", ...

  3. SCCM符合性设置

    符合性设置--可以针对注册表值.脚本.文件系统.补丁更新情况进行符合性检查,除了在报表中查看结果外,还可以在CCM客户端 的 配置 中查看符合性评估结果,适合排错1.配置项目---新建针对 注册表值. ...

  4. Windows Server 2003下ASP.NET无法识别IE11的解决方法

    由于IE11对User-Agent字符串进行了比较大的改动,所以导致很多通过User-Agent来识别浏览器的程序,都相应的出现了无法识别IE11的情况.(普通用户端则可以通过这个方法来进行设置.) ...

  5. winForm 程序开发界面参数传递

    1. using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; u ...

  6. Linux文件时间属性

    Linux文件时间属性                                                                                         ...

  7. Qt Quick实现的涂鸦程序

    之前一直以为 Qt Quick 里 Canvas 才干够自绘.后来发觉不是,原来还有好几种方式都能够画图! 能够使用原始的 OpenGL(Qt Quick 使用 OpenGL 渲染).能够构造QSGN ...

  8. C++检测一个文件是否存在

    ifstream::is_open - C++ Reference http://www.cplusplus.com/reference/fstream/ifstream/is_open/ // if ...

  9. IIS问题汇总

    1.问题描述   VS和Framework的安装顺序不对导致网站打不开   原因分析   Framework出现问题   解决办法   重新注册Framework版本   a.开始->运行-&g ...

  10. 基于jQuery美化联动下拉选择框

    今天给大家介绍一款基于jQuery美化联动下拉选择框.这款下下拉选择框js里自带了全国所有城市的数数库.下拉选择框适用浏览器:IE8.360.FireFox.Chrome.Safari.Opera.傲 ...