[287] Find the Duplicate Number | LeetCode Medium

1130 단어 leetcodeleetcode

문제설명

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.

제한사항
1 <= n <= 105
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:
    def findDuplicate(self, nums: List[int]) -> int:
        nums.sort()  #정렬
        for i in range(len(nums)):
            if(nums[i] == nums[i+1]) :
                return nums[i]

좋은 웹페이지 즐겨찾기