# [leetcode] 287. Find the Duplicate Number

Given an arraynums containingn + 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:

1. Youmust 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 thanO(n2).

4. There is only one duplicate number in the array, but it could be repeated more than once.

class Solution {
public:
int findDuplicate(vector<int>& nums) {
if(nums.size() < 1) return 0;

int fast = 0, slow = 0;

do {
slow = nums[slow];
fast = nums[nums[fast]];
} while(slow != fast);

fast = 0;
do {
slow = nums[slow];
fast = nums[fast];
} while(slow != fast);

return slow;
}
};

• 本文已收录于以下专栏：

## LeetCode 287. Find the Duplicate Number 题解（C++）

Given an array nums containing n + 1 integers where each integer is between 1 and n (inclusive), pro...

## 【LeetCode】287.Find the Duplicate Number解题报告

【LeetCode】287.Find the Duplicate Number解题报告tags: Array 题目地址：https://leetcode.com/problems/find-the-d...

## leetcode287. Find the Duplicate Number

Given an array nums containing n + 1 integers where each integer is between 1 and n (inclusive), pro...
• sxs11
• 2017年02月26日 22:38
• 100

## LeetCode 287. Find the Duplicate Number

LeetCode 287. Find the Duplicate Number 两种解法c++

## LeetCode题解(Week 1)：287. Find the Duplicate Number

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