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. Dubbo 生态添新兵,Dubbo Admin 发布 v0.1

    为了提升 Dubbo 里程碑版本2.7.0的使用体验,我们于去年年中启动了 Dubbo Admin 的重构计划,并作为Dubbo生态的子项目,于近期发布了v0.1,重构后的项目在结构上的变化如下: 将 ...

  2. Go语言?Docker?对新技术怎么看?

    对于 Go 语言和 Docker 这两种技术,在国内的技术圈中有相当大的一部分人和群体还在持观望或是不信任的态度.所以,我想写这篇文章,从两个方面来论述一下我的观点和看法. 上个月,作为 Go 语言的 ...

  3. 【NOIP 2018】保卫王国(动态dp / 倍增)

    题目链接 这个$dark$题,嗯,不想说了. 法一:动态$dp$ 虽然早有听闻动态$dp$,但到最近才学,如果你了解动态$dp$,那就能很轻松做出这道题了.故利用这题在这里科普一下动态$dp$的具体内 ...

  4. ASP.NET调用cmd命令提示符拒绝访问解决方案

    using System.Diagnostics; public class CmdHelper { private static string CmdPath = @"C:\Windows ...

  5. plsql批量导入sql文件

    背景:有时候在两个数据库之间导入导出数据,不可避免的需要进行sql文件的批量导入,一个个导入效率太低,所以可以考虑批量导入的办法进行导入. 操作步骤 1.假设有三个sql脚本,分别为aa.sql,bb ...

  6. NO.6: 若不想编译器提供自动生成的函数,就应该明确拒绝

    1.为驳回编译器自动生成函数的技能,可把这些函数的声明放入private,如果是继承类型可把base class的这些函数声明private,可在编译期间得到警告

  7. 生成ssh-key for GIthub

    在Github里,如果我们想通过ssh的方式进行身份验证,我们就需要建立ssh-key: 方法一: git GUI,点击help,选择Generate ssh key

  8. python---补充django中文报错(2),Django3.5出错

    今天是要Django3.5设置项目,结果出现中文报错,虽然之前分析过py2.7的报错原因,但是在py3之后reload不在使用,需要引入: from importlib import reload 但 ...

  9. JAVA-Servlet高级应用

    会话只是指一段指定的时间间隔. 会话跟踪是维护用户状态(数据)的一种方式.它也被称为servlet中的会话管理. Http协议是一个无状态的,所以我们需要使用会话跟踪技术来维护用户状态. 每次用户请求 ...

  10. bzoj千题计划205:bzoj3529: [Sdoi2014]数表

    http://www.lydsy.com/JudgeOnline/problem.php?id=3529 有一张n*m的数表,其第i行第j列(1 < =i < =n,1 < =j & ...