# leetcode 287. Find the Duplicate Number（python）

·  阅读 1421

Offer 驾到，掘友接招！我正在参与2022春招打卡活动，点击查看活动详情

### 描述

Given an array of integers nums containing n + 1 integers where each integer is in the range [1, n] inclusive. There is only one repeated number in nums, return this repeated number.

You must solve the problem without modifying the array nums and uses only constant extra space.

Example 1:

``````Input: nums = [1,3,4,2,2]
Output: 2

Example 2:

``````Input: nums = [3,1,3,4,2]
Output: 3

Note:

``````1 <= n <= 10^5
nums.length == n + 1
1 <= nums[i] <= n
All the integers in nums appear only once except for precisely one integer which appears two or more times.

### 解答

``````class Solution(object):
def findDuplicate(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
nums.sort()
for i in range(len(nums)-1):
if nums[i] == nums[i+1]:
return nums[i]

### 运行结果

``````Runtime: 644 ms, faster than 71.73% of Python online submissions for Find the Duplicate Number.
Memory Usage: 25.2 MB, less than 57.18% of Python online submissions for Find the Duplicate Number.

### 原题链接

leetcode.com/problems/fi…