64.Find the Duplicate Number(发现重复数字)
####Level:
Medium
####题目描述:
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.
Example 1:
Input: [1,3,4,2,2]
Output: 2
Example 2:
Input: [3,1,3,4,2]
Output: 3
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.
####思路分析:
和在一个循环链表中找两个相同元素的道理一样,设置一个快指针,一个慢指针,如果快指针和慢指针相同,则让快指针先走一步然后直到快指针的值等于 慢指针的值,则找到答案。
####代码:
public class Solution{
public int findDuplicate(int []nums){
int n=nums.length;
int slow=0;
int fast=1;
while(nums[slow]!=nums[fast]){
slow=(slow+1)%n;
fast=(fast+2)%n;
if(slow==fast){
fast=(fast+1)%n;
}
}
return nums[slow];
}
}
64.Find the Duplicate Number(发现重复数字)的更多相关文章
- LeetCode Find the Duplicate Number 找重复出现的数(技巧)
题意: 有一个含有n+1个元素的数组,元素值是在1-n之间的整数,请找出其中出现超过1次的数.(保证仅有1个出现次数是超过1的数) 思路: 方法一:O(nlogn).根据鸽笼原理及题意,每次如果< ...
- LeetCode 287. Find the Duplicate Number (找到重复的数字)
Given an array nums containing n + 1 integers where each integer is between 1 and n (inclusive), pro ...
- 287. Find the Duplicate Number 找出数组中的重复数字
[抄题]: Given an array nums containing n + 1 integers where each integer is between 1 and n (inclusive ...
- Find the Duplicate Number (寻找重复数字)
对于一个长度为n+1的数组,其中每一个值的取值范围是[1,n],可以证明的是必然存在一个重复数字(抽屉原理),假设仅存在一个重复数字,找到他. 举例:输入:[1,3,4,2,1],输出:1 自己做的时 ...
- [LeetCode] Find the Duplicate Number 寻找重复数
Given an array nums containing n + 1 integers where each integer is between 1 and n (inclusive), pro ...
- Leetcode Find the Duplicate Number
最容易想到的思路是新开一个长度为n的全零list p[1~n].依次从nums里读出数据,假设读出的是4, 就将p[4]从零改成1.如果发现已经是1了,那么这个4就已经出现过了,所以他就是重复的那个数 ...
- [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 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 保存已经访问过的数字 链表成环 二分查找 日期 题目 ...
- Find the Duplicate Number
Given an array nums containing n + 1 integers where each integer is between 1 and n (inclusive), pro ...
随机推荐
- 06.Linux-RedHat系统网卡服务连不上活跃连接路径变化
问题:在新装的系统中,重启网卡的时候出现如下报错 [root@localhost ~]# service network restart 正在关闭接口 eth0: 设备状态:3 (断开连接) [确定] ...
- rabbitmq 结构体
amqp_basic_properties_t props; props._flags = AMQP_BASIC_CONTENT_TYPE_FLAG | AMQP_BASIC_DELIVERY_MOD ...
- Sass:RGB颜色函数-Red()、Green()、Blue()函数
Red() 函数 red() 函数非常简单,其主要用来获取一个颜色当中的红色值.假设有一个 #f36 的颜色,如果你想得到 #f36 中的 red 值是多少,这个时候使用 red() 函数就能很简单获 ...
- 关于嵌入式linux下的串口通讯问题---需增加回车/换行才能接收
问题:在Linux应用层,直接从/dev/tty***使用read()函数读数据,无法读到,只有在数据末尾加上0a/0d才可以读到数据(这里是发送十六进制的数据,ASCLL码同理,增加回车才可以读到数 ...
- Python3.5-20190501-廖老师的
python是一门解释型\脚本语言(和js特别像,如果同时学习js和python完全搅浑了.) 在运行py时候是一句一句翻译成cpu识别的机器码,所以速度比较慢.而C程序是运行前直接编译成CPU能执行 ...
- 使用yum命令报错
树莓派(Raspberry Pi 3) centos7使用yum命令报错File "/usr/bin/yum", line 30 except KeyboardInterrupt, ...
- iview的Affix组件滚动时没有按照预期固定
业务场景 新建任务的页面,创建和重置按钮,页面没有滚动时,直接跟在内容下面:页面滚动时,固定于页面下方,不随内容进行滚动,以方便按钮的操作.效果如下: 问题以及解决办法 直接使用<Affix : ...
- iptables防火墙相关命令详解
前提基础: 当主机收到一个数据包后,数据包先在内核空间中处理,若发现目的地址是自身,则传到用户空间中交给对应的应用程序处理,若发现目的不是自身,则会将包丢弃或进行转发. iptables实现防火墙功能 ...
- delphi 一个关于xml文件导入数据库的问题
function LoadXml(shortPath:string;var xmlobj: IXMLDOMDocument):boolean; var tmpXml:IXMLDOMDOCUMENT; ...
- [CSP-S模拟测试]:beauty(搜索)
题目描述 距离产生美.一棵包含$n$个点的树,有$2k$个不同的关键点,我们现在需要将这些点两两配对,对于一种形如:$$(u_1,v_1),(u_2,v_2),...,(u_k,v_k)$$的配对方案 ...