## 安静的程序猿

Hold on well at the current situation,look forward to the future...

# 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:

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.

1） 排序后遍历：不符合条件1的要求，数组不可改；

2） map：不符合条件2的要求，O(n)的空间复杂度。

Dumplicate数。这样考虑是因为以为只会重复两次，但是题目并没有这么说，即如果存在多次（比如偶数次），最后还是可能转为正数。

public class Solution {
public int findDuplicate(int[] nums) {
int index, length = nums.length, result = 0;
for(int i = 0; i < length; i++) {
index = nums[i];
if(index < 0) {
index *= -1;
}
if(nums[index] < 0) {
result = index;
break;
} else {
nums[index] *= -1;
}
}
return result;
}
}

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

2016-01-01 11:34:20

#### leetcode -- Find the Duplicate Number-- 经典重点

2015-12-23 17:50:39

#### leetcode笔记：Find the Duplicate Number

2016-02-22 23:59:25

#### leetcode 287: Find the Duplicate Number

2015-09-29 08:29:12

#### LeetCode 287. Find the Duplicate Number（找重复数字）

2016-04-15 07:02:18

#### [LeetCode 287] Find the Duplicate Number

2015-09-29 11:51:19

#### 【LeetCode从零单刷】Find the Duplicate Number

2015-10-10 09:51:21

#### Find the Duplicate Number问题及解法

2017-09-14 16:27:49

#### 287. Find the Duplicate Number--binary search/快慢指针

2017-02-18 20:55:27

#### leetcode 287 Find the Duplicate Number

2015-10-24 23:08:35