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.
/*************************************************************************
> 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的更多相关文章
- LeetCode 287. Find the Duplicate Number (python 判断环,时间复杂度O(n))
LeetCode 287. Find the Duplicate Number 暴力解法 时间 O(nlog(n)),空间O(n),按题目中Note"只用O(1)的空间",照理是过 ...
- [LeetCode] 287. Find the Duplicate Number 寻找重复数
Given an array nums containing n + 1 integers where each integer is between 1 and n (inclusive), pro ...
- LeetCode 287. Find the Duplicate Number (找到重复的数字)
Given an array nums containing n + 1 integers where each integer is between 1 and n (inclusive), pro ...
- LeetCode | 287. 寻找重复数
特别感谢LeetCode大佬陈牧远的科普知识 给定一个包含 n + 1 个整数的数组 nums,其数字都在 1 到 n 之间(包括 1 和 n),可知至少存在一个重复的整数.假设只有一个重复的整数,找 ...
- Java实现 LeetCode 287 寻找重复数
287. 寻找重复数 给定一个包含 n + 1 个整数的数组 nums,其数字都在 1 到 n 之间(包括 1 和 n),可知至少存在一个重复的整数.假设只有一个重复的整数,找出这个重复的数. 示例 ...
- [LeetCode]287. 寻找重复数(二分)
题目 给定一个包含 n + 1 个整数的数组 nums,其数字都在 1 到 n 之间(包括 1 和 n),可知至少存在一个重复的整数.假设只有一个重复的整数,找出这个重复的数. 示例 1: 输入: [ ...
- LeetCode : 287. Find the Duplicate Number
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAACRAAAAMMCAYAAAAhQhmZAAAMFGlDQ1BJQ0MgUHJvZmlsZQAASImVlw ...
- [LeetCode] 287. Find the Duplicate Number 解题思路
Given an array nums containing n + 1 integers where each integer is between 1 and n (inclusive), pro ...
- leetcode 287寻找重复数
这道题用STL容器就很好写了,可以用set也可以用map, 用unordered_map的C++代码如下: class Solution { public: int findDuplicate(vec ...
随机推荐
- Castle IOC容器内幕故事(下)
主要内容 1.ComponentModelBuilder 和 Contributors 2.Contributors分析 3.Handles分析 4.ComponentActivator分析 一.Co ...
- 开发中,如何配合后端,保存你的静态html页
添加备注2015.4.8 最终决定采用相对路径方法, /img/img.jpg这种“绝对”路径写法必须在网站环境中才能识别,不利于静态页面的查看,故不予采用! 所以采用img/img.jpg或../i ...
- canvas绘制直线和多边形基本操作
<!DOCTYPE HTML> <html lang="en"> <body> <canvas id="canvas" ...
- hdu 4497 GCD and LCM 数学
GCD and LCM Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=4 ...
- Codeforces Gym 100187M M. Heaviside Function two pointer
M. Heaviside Function Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100187/ ...
- js 获取cookie
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head ...
- android自定义TabWidget样式
先看看效果图吧,个人觉得图标丑了点,不过还行,自己用PS做的 下面是全部代码和流程,一定要按流程顺序来,不然错误! 1.tabhost.xml <TabHost xmlns:android=&q ...
- IOS编程User Interface基础
IOS编程之User Interface基础 目录 概述 相关概念 常见问题 状态栏的隐藏 应用图标的设置 概述 IOS用户界面是APP呈现给用户最直观.最常用的方式,因此学会用户界面的编程是学习IO ...
- C/C++基础总结
1 static(静态)变量有什么作用 3个体明显的作用:1)在函数体内,静态变量具有“记忆”功能,即一个被声明为静态变量在一个函数被调用的过程中其值维持不变2)在模块内,它的作用域范围是有限制的,即 ...
- The Kernel Newbie Corner: Kernel Debugging with proc "Sequence" Files--Part 3
转载:https://www.linux.com/learn/linux-career-center/44184-the-kernel-newbie-corner-kernel-debugging-w ...