-
Leetcode 137. Single Number II개발/Leetcode 2023. 7. 4. 09:11
https://leetcode.com/problems/single-number-ii/description/
Single Number II - LeetCode
Can you solve this real interview question? Single Number II - Given an integer array nums where every element appears three times except for one, which appears exactly once. Find the single element and return it. You must implement a solution with a lin
leetcode.com
문제
정수 배열인 nums가 주어진다. 이 배열은 한 원소만 정확히 한번만 등장하고 나머지 원소는 3번씩 등장한다. 이때 한번만 등장하는 원소를 반환하여라
이 문제는 O(n)의 복잡도를 지닌 코드만 통과할 수 있습니다.
코드
class Solution:def singleNumber(self, nums: List[int]) -> int:m = {}for n in nums:if n not in m:m[n] = 0m[n] +=1
return list(filter(lambda x: m[x] == 1, m))[0]'개발 > Leetcode' 카테고리의 다른 글
Leetcode 209. Minimum Size Subarray Sum (0) 2023.07.06 Leetcode 1493. Longest Subarray of 1's After Deleting One Element (0) 2023.07.05 Leetcode 859. Buddy Strings (0) 2023.07.03 Leetcode 2114. Maximum Number of Words Found in Sentences (0) 2023.07.02 Leetcode 1678. Goal Parser Interpretation (0) 2023.07.02