herrDeng網內搜尋

自訂搜尋

Ads

2024年7月2日 星期二

Python C++速解multiset問題Leetcode 350 Intersection of Two Arrays II


Python C++速解multiset問題Leetcode 350  Intersection of Two Arrays II
兩個多重集合(multiset)的交集是兩個多重集中公共元素的多重集合。
Python code請進
The intersection of 2 multisets is the multiset of common elements in both multisets with multiplicites.
[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
        

沒有留言:

Related Posts Plugin for WordPress, Blogger...

熱門文章