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
    
  • 相关阅读:
    Poj3126
    Poj1426
    2806 红与黑
    3100 蜗牛
    1225 八数码难题
    2549 自然数和分解
    2547 东方辉针城
    2928 你缺什么
    1629 01迷宫
    1029 遍历问题
  • 原文地址:https://www.cnblogs.com/wangnanabuaa/p/6197460.html
Copyright © 2011-2022 走看看