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 ...
随机推荐
- jquery+ajax获取本地json对应数据
首先,记得导入jquery.js文件. json内容: var obj123=[ {"option":"2,3,9,14,19,24,32",&q ...
- XSLT学习(九)通过JavaScript转化xml
如果您的浏览器支持 XSLT,那么在浏览器中它可被用来将文档转换为 XHTML. JavaScript 解决方案 在前面的章节,我们已向您讲解如何使用 XSLT 将某个 XML 文档转换为 XHTML ...
- 06.Linux-RedHat系统网卡服务连不上活跃连接路径变化
问题:在新装的系统中,重启网卡的时候出现如下报错 [root@localhost ~]# service network restart 正在关闭接口 eth0: 设备状态:3 (断开连接) [确定] ...
- 164-基于TI DSP TMS320C6455和Altera FPGA EP2S130的Full CameraLink PDS150接口板卡
一.板卡概述 本板卡由我公司自主研发,板卡采用DSP+FPGA的结构,DSP使用TMS320C6455芯片,FPGA采用ALTERA的高端FPGA芯片Stratix II EP2S系列EP2S130, ...
- 微信小程序(15)--上传图片公用组件(2)
接下来开始写写上传图片的公用组件,可以自定义上传几张图片. chooseImage文件夹里面的index.wxml和index.js,涉及图片上传,删除,预览. <view class=&quo ...
- .bat 文件调用python脚本
1.将clearlog.py 脚本放在指定目录 比如 我放在 C:\Users\Administrator\Desktop 上 也就是桌面上 2.创建一个.bat 位后缀名的脚本 3.写入如下脚本 @ ...
- Docker的使用(未完待续)
一.帮助命令 docker version docker info docker --help 二.镜像命令 列出机器上所有的镜像 docker images 查找某个镜像 docker search ...
- Sass-颜色运算
所有算数运算都支持颜色值,并且是分段运算的.也就是说,红.绿和蓝各颜色分段单独进行运算.如: p { color: #010203 + #040506; } 计算公式为 01 + 04 = 05.02 ...
- java访问ftp
1.连接ftp FTPClient ftpClient = new FTPClient(); ftpClient.connect(host,port); ftpClient.login(userNam ...
- [CodeForces - 1225E]Rock Is Push 【dp】【前缀和】
[CodeForces - 1225E]Rock Is Push [dp][前缀和] 标签:题解 codeforces题解 dp 前缀和 题目描述 Time limit 2000 ms Memory ...