[287] Find the Duplicate Number | LeetCode Medium

yoongyum·2022년 3월 30일
0

코딩테스트 🧩

목록 보기
2/47
post-thumbnail

🔎 문제설명

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]

0개의 댓글