Difficulty:medium

 More:【目录】LeetCode Java实现

Description

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:

  1. You must not modify the array (assume the array is read only).
  2. You must use only constant, O(1) extra space.
  3. Your runtime complexity should be less than O(n2).
  4. There is only one duplicate number in the array, but it could be repeated more than once.

Intuition

we can regard the array as a linked list: treat the index as a node, treat the value as a next pointer. For example: (array =>linked List)

↓↓

So we can transform this problem into a Linked List Cycle problem, refer to Linked List Cycle II for more details.

Solution

    public int findDuplicate(int[] nums) {
if(nums==null)
return -1; //invalid
int fast=0;
int slow=0;
do{
fast=nums[nums[fast]];
slow=nums[slow];
}while(fast!=slow);
int entry=0;
while(entry!=slow){
entry=nums[entry];
slow=nums[slow];
}
return entry;
}

  

Complexity

Time complexity : O(n)

Space complexity : O(1)

What I've learned

there is a cycle in the list, becasue:

1. Two different indexes have the same value, which means two different nodes point to the same node.

2. node0 (index=0) will absolutely point to another node.

 More:【目录】LeetCode Java实现

【LeetCode】287. Find the Duplicate Number的更多相关文章

  1. 【LeetCode】287. Find the Duplicate Number 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 保存已经访问过的数字 链表成环 二分查找 日期 题目 ...

  2. 【Leetcode】287. 寻找重复数(数组模拟链表的快慢指针法)

    寻找重复数 根据题意,数组中的数字都在1~n之间,所以数字的范围是小于数组的范围的,数组的元素可以和数组的索引相联系. 例如:nums[0] = 1 即可以将nums[0]作为索引 通过nums[0] ...

  3. 【LeetCode】1150. Check If a Number Is Majority Element in a Sorted Array 解题报告(C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典 二分查找 日期 题目地址:https://lee ...

  4. 【leetcode】287. 寻找重复数

    题目链接:传送门 题目描述: 给定一个数组 nums 包含 n + 1 个整数,每个整数在 1 到 n 之间,包括 1 和 n.现在假设数组中存在一个重复的数字,找到该重复的数字. 注意 不能修改数组 ...

  5. 【LeetCode】217 & 219 - Contains Duplicate & Contains Duplicate II

     217 - Contains Duplicate Given an array of integers, find if the array contains any duplicates. You ...

  6. 【LeetCode】171. Excel Sheet Column Number 解题报告(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目大意 解题方法 Java解法 Python解法 日期 [LeetCode] 题 ...

  7. 【LeetCode】476. 数字的补数 Number Complement

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:Leetcode, 力扣,476, 补数,二进制,Pyth ...

  8. 【LeetCode】190 & 191 - Reverse Bits & Number of 1 Bits

    190 - Reverse Bits Reverse bits of a given 32 bits unsigned integer. For example, given input 432615 ...

  9. 【LeetCode】9 & 234 & 206 - Palindrome Number & Palindrome Linked List & Reverse Linked List

    9 - Palindrome Number Determine whether an integer is a palindrome. Do this without extra space. Som ...

随机推荐

  1. WEB下载显示下载名称乱码--java

    将文件名编码转换为ISO8859-1即可,如下 downloadFileName = new String(fileName.getBytes("gbk"), "ISO8 ...

  2. 安装hadoop1.2.1(参考hadoop实战第二版)

    准备: 安装jdk和ssh. 1. 官网下载hadoop1.2.1 hadoop-1.2.1.tar.gz http://hadoop.apache.org/releases.html 2. 解压到/ ...

  3. Java基础-赋值运算符Assignment Operators与条件运算符Condition Operators

    Java基础-赋值运算符Assignment Operators与条件运算符Condition Operators 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.赋值运算符 表 ...

  4. MySQL的备份和恢复-mysqldump

    MySQL的备份和恢复-mysqldump 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.为什么需要备份 1>.做灾难恢复 天有不测风云嘛,如果你的服务器被黑客攻击了(比 ...

  5. 错误 1 无法将文件“obj\Debug\XXX.exe”复制到“bin\Debug\XXX.exe”。文件“bin\Debug\XXX.exe”正由另一进程使用,因此该进程无法访问该文件

    在重新生成Windows服务的时候出现的这个问题,原因是因为你的Windows服务已经在运行了,你可以卸载掉这个服务,也可以在资源管理器里直接关闭.

  6. telegraf、influxDB、Grafana的安装与基本使用

    目的理解influxDB的数据收集原理和方法为使用grafana分析数据及展示结作好准备介绍[收集数据] Telegraf 是一个用 Go 编写的代理程序,可收集系统和服务的统计数据,并写入到 Inf ...

  7. 就for循环VS for-in循环

    这种模式的问题在于每次循环迭代的时候都要访问数据的长度.这样会使代码变慢,特别是当myarray不是数据,而是HTML容器对象时. HTML容器是DOM方法返回的对象,如: document.getE ...

  8. 01:MFC应用程序编程

    一 MFC的发展 VC 1.0->VC 5.0->VC 6.0->VC2008 SP1)->VS2010 二 MFC基础 1 MFC 微软基础类库 采用类的方式,将Win32 ...

  9. C++ 修饰符类型

    C++ 修饰符类型 C++ 允许在 char.int 和 double 数据类型前放置修饰符.修饰符用于改变基本类型的含义,所以它更能满足各种情境的需求. 下面列出了数据类型修饰符: signed u ...

  10. Shell中各种判断语法

    Shell判断 按照文件类型进行判断 -b 判断文件是否存在,并且是否为快设备文件(是块设备文件为真) -c 判断文件是否存在,并且是否为字符设备文件(是字符设备文件为真) -d 判断文件是否存在,并 ...