Python C++速解multiset問題Leetcode 350  Intersection of Two Arrays II
兩個多重集合(multiset)的交集是兩個多重集中公共元素的多重集合。
The intersection of 2 multisets is the multiset of common elements in both multisets with multiplicites.
[code on Leetcode]https://leetcode.com/problems/intersection-of-two-arrays-ii/solutions/5399800/use-1-freq-count-decrease-vs-2-pointers-binary-search-0ms-beats-100/
[Python  C++集合論Cantor對角線法解Leetcode 1980  Find Unique Binary String]https://www.youtube.com/watch?v=0iFR8nafMWE
class Solution:
    def intersect(self, nums1: List[int], nums2: List[int]) -> List[int]:
        freq=[0]*1001
        for x in nums1:
            freq[x]+=1
        ans=[]
        for x in nums2:
            if  freq[x]>0:
                ans.append(x)
                freq[x]-=1
        return ans
        
 
   
 
 
沒有留言:
張貼留言