【LeetCode】287. Find the Duplicate Number
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:
- 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.
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的更多相关文章
- 【LeetCode】287. Find the Duplicate Number 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 保存已经访问过的数字 链表成环 二分查找 日期 题目 ...
- 【Leetcode】287. 寻找重复数(数组模拟链表的快慢指针法)
寻找重复数 根据题意,数组中的数字都在1~n之间,所以数字的范围是小于数组的范围的,数组的元素可以和数组的索引相联系. 例如:nums[0] = 1 即可以将nums[0]作为索引 通过nums[0] ...
- 【LeetCode】1150. Check If a Number Is Majority Element in a Sorted Array 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典 二分查找 日期 题目地址:https://lee ...
- 【leetcode】287. 寻找重复数
题目链接:传送门 题目描述: 给定一个数组 nums 包含 n + 1 个整数,每个整数在 1 到 n 之间,包括 1 和 n.现在假设数组中存在一个重复的数字,找到该重复的数字. 注意 不能修改数组 ...
- 【LeetCode】217 & 219 - Contains Duplicate & Contains Duplicate II
217 - Contains Duplicate Given an array of integers, find if the array contains any duplicates. You ...
- 【LeetCode】171. Excel Sheet Column Number 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目大意 解题方法 Java解法 Python解法 日期 [LeetCode] 题 ...
- 【LeetCode】476. 数字的补数 Number Complement
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:Leetcode, 力扣,476, 补数,二进制,Pyth ...
- 【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 ...
- 【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 ...
随机推荐
- c++函数写的都对,还是说incompatible或者not found的解决办法
vs2010,c++,定义了一个函数如下,在BianHuanYuDib.h文件中: 在BianHuanYuDib.cpp中: 写的完全正确,但还是会报错: 很明显,连std都报错了,一般不是真的有很大 ...
- C++并发编程实战---阅读笔记
1. 当把函数对象传入到线程构造函数中时,需要避免“最令人头痛的语法解析”.如果传递了一个临时变量,而不是一个命名的变量:C++编译器会将其解析为函数声明,而不是类型对象的定义. 例如: class ...
- CSS框架960Grid从入门到精通一步登天
http://blog.chinaunix.net/uid-22414998-id-2878529.html 1.什么是CSS框架? CSS框架是一种你能够使用在你的web项目中概念上的结构, ...
- hdu5583
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <math.h> ...
- Jenkins-Pipeline 流水线发布
基于docker部署 1.部署jenkins $ yum -y install java $ java -version openjdk version "1.8.0_181" O ...
- for in,Object.keys和Object.getOwnPropertyNames的区别
var parent = Object.create(Object.prototype, { a: { value: 1, writable: true, enumerable: true, conf ...
- node爬虫进阶版
手写了一个方便爬虫的小库: const url = require('url') const glib = require('zlib') //默认头部 const _default_headers ...
- P2889 [USACO07NOV]挤奶的时间Milking Time
P2889 [USACO07NOV]挤奶的时间Milking Time 奶牛Bessie在0~N时间段产奶.农夫约翰有M个时间段可以挤奶,时间段f,t内Bessie能挤到的牛奶量e.奶牛产奶后需要休息 ...
- SQL记录-PLSQL变量与常量文字
PL/SQL变量 变量是只不过是一个给定的存储区域,程序可以操纵的名称.PL/SQL每个变量具有一个特定的数据类型,它决定了大小和变量的存储器的值,可以说存储器和设置操作可以施加到可变内被存储的范 ...
- 项目引入非配置的文件,打成war包后测试报错的可能原因
写在前边 这阵子有点忙,开发一个微服务项目中读取配置文件的时候在本地测试是可以的,但是一到测试环境就报错,经查看发现是因为发布的时候是用的war包,使用java -jar xxx.war启动的,所以用 ...