首页 > 其他 > 详细

【leetcode】1213.Intersection of Three Sorted Arrays

时间:2019-10-07 09:10:05      阅读:148      评论:0      收藏:0      [点我收藏+]

题目如下:

Given three integer arrays arr1arr2 and arr3 sorted in strictly increasing order, return a sorted array of only the integers that appeared in all three arrays.

 

Example 1:

Input: arr1 = [1,2,3,4,5], arr2 = [1,2,5,7,9], arr3 = [1,3,4,5,8]
Output: [1,5]
Explanation: Only 1 and 5 appeared in the three arrays.

 

Constraints:

  • 1 <= arr1.length, arr2.length, arr3.length <= 1000
  • 1 <= arr1[i], arr2[i], arr3[i] <= 2000

解题思路:【leetcode&CN&竞赛】1198.Find Smallest Common Element in All Rows 类似,但是本题约定了每个数组中的元素是唯一的,所以只需要遍历三个数组,计算出每个元素出现的次数即可。

代码如下:

class Solution(object):
    def arraysIntersection(self, arr1, arr2, arr3):
        """
        :type arr1: List[int]
        :type arr2: List[int]
        :type arr3: List[int]
        :rtype: List[int]
        """
        val = [0] * 2001
        for i in arr1:
            val[i] += 1
        for i in arr2:
            val[i] += 1
        for i in arr3:
            val[i] += 1
        res = []
        for i,v in enumerate(val):
            if v == 3:res.append(i)
        return res

 

【leetcode】1213.Intersection of Three Sorted Arrays

原文:https://www.cnblogs.com/seyjs/p/11629272.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!