관리 메뉴

솜씨좋은장씨

[leetCode] 260. Single Number III (Python) 본문

Programming/코딩 1일 1문제

[leetCode] 260. Single Number III (Python)

솜씨좋은장씨 2020. 7. 29. 00:28
728x90
반응형

Given an array of numbers nums, in which exactly two elements appear only once and all the other elements appear exactly twice. Find the two elements that appear only once.

 

Example:

Input:  [1,2,1,3,2,5]
Output: [3,5]

Note:

  1. The order of the result is not important. So in the above example, [5, 3] is also correct.
  2. Your algorithm should run in linear runtime complexity. Could you implement it using only constant space complexity?

 

Solution

from collections import Counter
class Solution:
    def singleNumber(self, nums: List[int]) -> List[int]:
        cnt = Counter(nums)
        
        cnt_list = list(cnt.items())
        
        answer = [x[0] for x in cnt_list if x[1] == 1]
        
        return answer

 

 

SOMJANG/CODINGTEST_PRACTICE

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

github.com

 

Comments