Leetcode 350. Intersection of Two Arrays II

Mingyu Jeon·2020년 4월 27일
0
post-thumbnail

# Runtime: 40 ms, faster than 94.91% of Python3 online submissions for Intersection of Two Arrays II.
# Memory Usage: 14.2 MB, less than 5.72% of Python3 online submissions for Intersection of Two Arrays II.

from collections import Counter
class Solution:
    def intersect(self, nums1: List[int], nums2: List[int]) -> List[int]:
        sorted(nums1)
        sorted(nums2)
        if len(nums1) < len(nums2):
            temp = nums1
            comp = nums2
        else:
            temp = nums2
            comp = nums1
            
        num_count = Counter(temp)
        ans = []
        for i in range(len(comp)):
            if comp[i] in num_count and num_count[comp[i]] > 0:
                num_count[comp[i]] -= 1
                ans.append(comp[i])
        return ans

https://leetcode.com/problems/intersection-of-two-arrays-ii/

0개의 댓글