zoukankan      html  css  js  c++  java
  • 两数组的交 II

    题目描述:
    计算两个数组的交

    题目描述:
    计算两个数组的交

    注意事项

    每个元素出现次数得和在数组里一样
    答案可以以任意顺序给出

    样例:
    nums1 = [1, 2, 2, 1], nums2 = [2, 2], 返回 [2, 2].

    Python代码

    class Solution:
        # @param {int[]} nums1 an integer array
        # @param {int[]} nums2 an integer array
        # @return {int[]} an integer array
        def intersection(self, nums1, nums2):
            # Write your code here
         
            nums1 = sorted(nums1)
            nums2 = sorted(nums2)
            ans = []
            i = 0
            j = 0
            while i < len(nums1) and j < len(nums2):
                if nums1[i] < nums2[j]:
                    i += 1
                elif nums1[i] > nums2[j]:
                    j +=1
                else:
                    ans+=nums1[i],
                    i += 1
                    j += 1
            return ans
    
  • 相关阅读:
    Pick-up sticks
    The Doors
    Intersecting Lines
    Segments
    TOYS
    Palindrome
    Distinct Substrings
    Milk Patterns
    Musical Theme
    JavaScript基于时间的动画算法
  • 原文地址:https://www.cnblogs.com/wangnanabuaa/p/6197460.html
Copyright © 2011-2022 走看看