관리 메뉴

솜씨좋은장씨

[leetCode] 645. Set Mismatch (Python) 본문

Programming/코딩 1일 1문제

[leetCode] 645. Set Mismatch (Python)

솜씨좋은장씨 2020. 9. 19. 22:00
728x90
반응형

The set S originally contains numbers from 1 to n. But unfortunately, due to the data error, one of the numbers in the set got duplicated to another number in the set, which results in repetition of one number and loss of another number.

Given an array nums representing the data status of this set after the error. Your task is to firstly find the number occurs twice and then find the number that is missing. Return them in the form of an array.

 

Example 1:

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

Note:

  1. The given array size will in the range [2, 10000].
  2. The given array's numbers won't have any order.

Solution

from collections import Counter

class Solution:
    def findErrorNums(self, nums: List[int]) -> List[int]:
        cnt = Counter(nums)
        
        most_1 = cnt.most_common(1)[0][0]
        
        range_set = set(list(range(1, len(nums)+1)))
        
        num_set = set(nums)
        
        miss_num = range_set - num_set
        
        
        if len(list(miss_num)) != 0:
            miss_num = list(miss_num)[0]
        
        return [most_1, miss_num]

 

SOMJANG/CODINGTEST_PRACTICE

1일 1문제 since 2020.02.07. Contribute to SOMJANG/CODINGTEST_PRACTICE development by creating an account on GitHub.

github.com

Comments