287. 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.
Credits:
Special thanks to @jianchao.li.fighter for adding this problem and creating all test cases.

public class Solution {
public int findDuplicate(int[] nums) {
int low = 1; int high = nums.length-1;

while(low<high){

int mid = low + (high-low)/2;
int cnt = 0;
for(int num:nums){
if(num <= mid)
cnt++;
}
if(cnt <= mid)
{
low = mid+1;
}else{
high = mid;
}

}
return low;
}
}
• 本文已收录于以下专栏：

［Leetcode］287. Find the Duplicate Number简单解法及解释 双指针复杂度O(n)

• u011613729
• 2016年01月01日 11:34
• 4456

leetcode 【287 Find the Duplicate Number】【Python】

• u014251967
• 2016年09月09日 11:19
• 268

287. Find the Duplicate Number。

Given an array nums containing n + 1 integers where each integer is between 1 and n (inclusive), pro...
• Leafage_M
• 2018年01月26日 00:07
• 97

287. Find the Duplicate Number

Problem Given an array nums containing n + 1 integers where each integer is between 1 and n...
• Scarlett_leetcode
• 2016年03月06日 11:45
• 147

287. Find the Duplicate Number***

Given an array nums containing n + 1 integers where each integer is between 1 and n (inclusive), pro...
• shanshanhi
• 2017年01月04日 10:59
• 162

LeetCode 287. Find the Duplicate Number

Given an array nums containing n + 1 integers where each integer is between 1 and n (inclusive), pro...
• github_34333284
• 2016年04月11日 03:22
• 100

[LeetCode] 287. Find the Duplicate Number

[LeetCode] 287. Find the Duplicate Number(Hard) 题目解答的几种算法
• zhao_tw
• 2016年09月06日 19:52
• 102

[LeetCode]287. Find the Duplicate Number

Given an array nums containing n + 1 integers where each integer is between 1 and n (inclusive), pro...
• Winter_ann
• 2016年03月06日 11:33
• 84

287. Find the Duplicate Number #Hard

leetcode 287. Find the Duplicate Number #Hard Given an array nums containing n + 1 integers where e...
• qhy936
• 2016年06月06日 08:35
• 116

287. Find the Duplicate Number(unsolved)

Given an array nums containing n + 1 integers where each integer is between 1 and n (inclusive), pro...
• gdmmzmj
• 2017年03月26日 13:30
• 116

举报原因： 您举报文章：287. Find the Duplicate Number 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)