zoukankan      html  css  js  c++  java
  • 349. 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]
            """
            return list(set(nums1)&set(nums2))
    class Solution(object):
        def intersection(self, nums1, nums2):
            """
            :type nums1: List[int]
            :type nums2: List[int]
            :rtype: List[int]
            """
            rlist=[]
            i=j=0
            nums1.sort()
            nums2.sort()
            n=len(nums1)
            m=len(nums2)
            while i<n and j<m:
                if nums1[i]<nums2[j]:
                    i+=1
                elif nums1[i]>nums2[j]:
                    j+=1
                else:
                    if len(rlist)==0 or rlist[-1]!=nums1[i]:
                        rlist.append(nums1[i])
                    i+=1
                    j+=1
            return rlist
  • 相关阅读:
    BZOJ3631 [JLOI2014] 松鼠的新家
    HDU
    HDU
    HDU
    二分图求最大独立集模板
    HDU
    HDU
    HDU
    Codeforces 1197F Coloring Game 矩阵快速幂 (看题解)
    HDU
  • 原文地址:https://www.cnblogs.com/rocksolid/p/6289602.html
Copyright © 2011-2022 走看看