Leetcode350 Intersection of Two Arrays II &Leetcode Intersection of Two Arrays

来源:互联网 发布:胡克霍根身体数据 编辑:程序博客网 时间:2024/06/05 12:46

Given two arrays, write a function to compute their intersection.

Example:
Given nums1 = [1, 2, 2, 1]nums2 = [2, 2], return [2, 2].

Note:

  • Each element in the result should appear as many times as it shows in both arrays.
  • The result can be in any order.

Follow up:

  • What if the given array is already sorted? How would you optimize your algorithm?
  • What if nums1's size is small compared to nums2's size? Which algorithm is better?
  • What if elements of nums2 are stored on disk, and the memory is limited such that you cannot load all elements into the memory at once?
题意:求两个数组的交集
解法:
击败了60%+的答案
class Solution(object):    def intersect(self, nums1, nums2):        """        :type nums1: List[int]        :type nums2: List[int]        :rtype: List[int]        """        C = collections.Counter        return list((C(nums1) & C(nums2)).elements()) #&代表求交集运算

这个解法的牛逼在于~惭愧,我第一次知道有Collections这个类
Counter是Collections这个类的一个子类,详情可参考 http://www.pythoner.com/205.html

例子:
import collectionsnums1=[3,1,4,4,2,2,]nums2=[3,3,5,6,2,2,2]C = collections.Counter

C(nums1)
Out[1]: Counter({1: 1, 2: 2, 3: 1, 4: 2})

C(nums2)
Out[2]: Counter({2: 3, 3: 2, 5: 1, 6: 1})

C(nums1)&C(nums2)
Out[3]: Counter({2: 2, 3: 1})

list((C(nums1)&C(nums2)).elements())
Out[11]: [2, 2, 3]

这道题里最快的答案:

class Solution(object):    def intersect(self, nums1, nums2):        """        :type nums1: List[int]        :type nums2: List[int]        :rtype: List[int]        """        maps={}        for n in nums1:            if n in maps:                maps[n]+=1            else:                maps[n]=1        result=[]        for i in range(len(nums2)):            if nums2[i] in maps and maps[nums2[i]]>0:#>0其实是在取交集                result.append(nums2[i])                maps[nums2[i]]-=1        return result

时间复杂度:O(n)
思路:对于nums1,把 “数字-出现次数“这个键值对保存在maps字典里
然后遍历nums2,如果nums2的某数字出现在maps1里且maps里该数字剩余的出现次数>0,就表示对于nums2里的该数字而言,它是与nums1的交集,所以放到结果result列里。然后maps[]里把该数字的剩余出现次数-1(代表该数字已经放在结果集里/代表nums2里的该数字已经被考虑过)

Leetcode349 Intersection of Two Arrays


Given two arrays, write a function to compute their intersection.

Example:
Given nums1 = [1, 2, 2, 1]nums2 = [2, 2], return [2].

Note:

  • Each element in the result must be unique.
  • The result can be in any order.



class Solution(object):    def intersection(self, nums1, nums2):        """        :type nums1: List[int]        :type nums2: List[int]        :rtype: List[int]        """        nums1=set(nums1)        nums2=set(nums2)        return list(nums1&nums2)

注意  & 其实已经是求交集了,但是对两个set求交集,结果仍是set
要求返回list, 所以list()这样强制类型转换

解法二:我自己参考上题的解法 ,不用set()函数(超级慢,only beat 5.38%的用户)
 
class Solution(object):    def intersection(self, nums1, nums2):        """        :type nums1: List[int]        :type nums2: List[int]        :rtype: List[int]        """        maps=[]        for n in nums1:            if n not in maps:                maps.append(n)        result=[]        for i in range(len(nums2)):            if nums2[i] in maps  and nums2[i] not in result:                result.append(nums2[i])                    return result


原创粉丝点击