관리 메뉴

솜씨좋은장씨

[leetCode] 137. Single Number II (Python) 본문

Programming/코딩 1일 1문제

[leetCode] 137. Single Number II (Python)

솜씨좋은장씨 2020. 7. 20. 01:56
728x90
반응형

Given a non-empty array of integers, every element appears three times except for one, which appears exactly once. Find that single one.

 

Note:

Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory?

 

Example 1:

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

Example 2:

Input: [0,1,0,1,0,1,99]
Output: 99

Solution

from collections import Counter

class Solution:
    def singleNumber(self, nums: List[int]) -> int:
        answer = 0
        cnt = dict(Counter(nums))
        
        keys = cnt.keys()
        
        for key in keys:
            if cnt[key] == 1:
                answer = key
        
        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