Leetcode Find the Duplicate Number
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.
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.
 
最容易想到的思路是新开一个长度为n的全零list p[1~n]。依次从nums里读出数据,假设读出的是4, 就将p[4]从零改成1。如果发现已经是1了,那么这个4就已经出现过了,所以他就是重复的那个数。这个解法的时间复杂度是O(N)。但是由于本题要求空间复杂度是O(1)。所以不能用。
可以用二分法,low = 1, high = n, mid = (left + right)//2,如果<=mid 的元素个数 > mid,那么重复的数字一定在[1, mid]区间内。反之,则一定在[mid+1, high]里面。注意红色的>mid不能是>=。
例如 【1,2, 2, 3(mid), 4, 5, 6】,【1,2, 3, 3(mid), 4, 5】
class Solution(object):
def findDuplicate(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
left = 1
right = len(nums) - 1 while left < right:
mid = (left + right)//2
count = 0
for x in nums:
if x <= mid:
count += 1 if count > mid:
right = mid
else:
left = mid + 1 return left
第二种解法来自:http://www.cnblogs.com/grandyang/p/4843654.html
使用类似Linked List Cycle II的思路。
def findDuplicate(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
slow = 0
fast = 0
t = 0
while True:
slow = nums[slow]
fast = nums[nums[fast]]
if slow == fast:
break
while True:
slow = nums[slow]
t = nums[t]
if slow == t:
break
return slow
Leetcode Find the Duplicate Number的更多相关文章
- [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
		
Description: Given an array nums containing n + 1 integers where each integer is between 1 and n (in ...
 - 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 ...
 - 【LeetCode】287. Find the Duplicate Number
		
Difficulty:medium More:[目录]LeetCode Java实现 Description Given an array nums containing n + 1 integer ...
 - LeetCode 287. Find the Duplicate Number (python 判断环,时间复杂度O(n))
		
LeetCode 287. Find the Duplicate Number 暴力解法 时间 O(nlog(n)),空间O(n),按题目中Note"只用O(1)的空间",照理是过 ...
 - Leetcode之二分法专题-287. 寻找重复数(Find the Duplicate Number)
		
Leetcode之二分法专题-287. 寻找重复数(Find the Duplicate Number) 给定一个包含 n + 1 个整数的数组 nums,其数字都在 1 到 n 之间(包括 1 和 ...
 - [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/ 目录 题目描述 题目大意 解题方法 保存已经访问过的数字 链表成环 二分查找 日期 题目 ...
 
随机推荐
- 泛型中? super T和? extends T的区别
			
原文出处: 并发编程网 经常发现有List<? super T>.Set<? extends T>的声明,是什么意思呢?<? super T>表示包括T在内的任何T ...
 - codevs 1215 迷宫
			
时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题目描述 Description 已知 n 个整数 x1,x2,-,xn,以及一个整数 k(k<n).从 n ...
 - Construct Binary Tree from Inorder and Postorder Traversal
			
Construct Binary Tree from Inorder and Postorder Traversal Given inorder and postorder traversal of ...
 - DWZ-JUI 树形Checkbox组件 无法一次获取所有选中的值的解决方法
			
UI中 tree Checkbox 组件 在官方文档中提供的oncheck事件中只能够获取当前点击的权限值,而无法获取其他选中的值 <ul class="tree treeFolder ...
 - WPF Adorner+附加属性 实现控件友好提示
			
标题太空泛,直接上图 无论是在验证啊,还是提示方面等一些右上角的角标之类的效果,我们会怎么做? 这里介绍一种稍微简单一些的方法,利用附加属性和Adorner来完成. 例如WPF自带的控件上要加这样的效 ...
 - [BZOJ1264][AHOI2006]Match(DP+树状数组)
			
题目:http://www.lydsy.com:808/JudgeOnline/problem.php?id=1264 分析: 考虑做一般的LCS的时候,更新结果的条件是a[i]==b[j]时候 于是 ...
 - exgcd,求乘法逆元
			
procedure exgcd(a,b:int64); var t:longint; begin then begin x:=;y:=; exit; end else exgcd(b,a mod b) ...
 - android之短信拦截器
			
下面通过短信拦截器来介绍短信中的广播 布局文件 在布局文件中可以设置需要拦截的号码 <?xml version="1.0" encoding="utf-8" ...
 - md5加密31位
			
今天将其它服务器里的用户数据导入到新的系统数据库中 出现密码不匹配情况 查看原来数据库中密码得到结果位: 原服务器密码 明文 正确32位密闻 67b14728ad9902aecba32e22fa4f6 ...
 - Linux配置无线网卡驱动实现无线上网
			
本机装Linux,需要配置的无线驱动.一般Ubuntu都集成无线驱动,基本上无线可以直接使用! 01.查看无线网卡的型号 [root@Mr-zhao software]# lspci | gre ...