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
  • 相关阅读:
    Iterator 迭代器
    Collection-List
    Collection-Set
    Collection
    多线程
    面向对象<高级>知识点
    链表
    面向对象<基础>知识点
    三层架构和MVC模式详解
    impala为什么比hive快
  • 原文地址:https://www.cnblogs.com/rocksolid/p/6289602.html
Copyright © 2011-2022 走看看