소프트웨어에 대한 모든 것

LeetCode 풀기 - 2032. Two Out of Three 본문

알고리즘/LeetCode

LeetCode 풀기 - 2032. Two Out of Three

앤테바 2021. 12. 18. 10:15
반응형

2032. Two Out of Three

https://leetcode.com/problems/two-out-of-three/

 

Two Out of Three - LeetCode

Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

leetcode.com

문제)

솔루션1)

class Solution:
    def twoOutOfThree(self, nums1: List[int], nums2: List[int], nums3: List[int]) -> List[int]:
        nums = list(set(nums1)) + list(set(nums2)) + list(set(nums3))
        counter = Counter(nums)
        return [k for k, v in counter.items() if v >=2]

솔루션2)

set의 집합 연산 사용

class Solution:
    def twoOutOfThree(self, nums1: List[int], nums2: List[int], nums3: List[int]) -> List[int]:
        a, b, c = set(nums1), set(nums2), set(nums3)
        return (a & b) | (a & c) | (b & c)

 

반응형
Comments