题目描述:

Given two arrays, write a function to compute their intersection.

Example:
Given nums1 = [1, 2, 2, 1]nums2 = [2, 2], return [2, 2].

Note:

    • Each element in the result should appear as many times as it shows in both arrays.
    • The result can be in any order.

解题思路:

排序,对比

代码如下:

class Solution(object):
def intersect(self, nums1, nums2):
"""
:type nums1: List[int]
:type nums2: List[int]
:rtype: List[int]
"""
nums1.sort()
nums2.sort()
index_1 = 0
index_2 = 0
inter = [] while index_1 < len(nums1) and index_2 < len(nums2):
if nums1[index_1] == nums2[index_2]:
inter.append(nums1[index_1])
index_1 += 1
index_2 += 1
elif nums1[index_1] < nums2[index_2]:
index_1 += 1
else:
index_2 += 1 return inter

  

Python [Leetcode 350]Intersection of Two Arrays II的更多相关文章

  1. 26. leetcode 350. Intersection of Two Arrays II

    350. Intersection of Two Arrays II Given two arrays, write a function to compute their intersection. ...

  2. [LeetCode] 350. Intersection of Two Arrays II 两个数组相交II

    Given two arrays, write a function to compute their intersection. Example 1: Input: nums1 = [1,2,2,1 ...

  3. [LeetCode] 350. Intersection of Two Arrays II 两个数组相交之二

    Given two arrays, write a function to compute their intersection. Example 1: Input: nums1 = [1,2,2,1 ...

  4. [LeetCode&Python] Problem 350. Intersection of Two Arrays II

    Given two arrays, write a function to compute their intersection. Example 1: Input: nums1 = [1,2,2,1 ...

  5. 【leetcode❤python】350. Intersection of Two Arrays II

    #-*- coding: UTF-8 -*- class Solution(object):    def intersect(self, nums1, nums2):                ...

  6. LeetCode 350. Intersection of Two Arrays II (两个数组的相交之二)

    Given two arrays, write a function to compute their intersection. Example:Given nums1 = [1, 2, 2, 1] ...

  7. LeetCode 350. Intersection of Two Arrays II

    Given two arrays, write a function to compute their intersection. Example:Given nums1 = [1, 2, 2, 1] ...

  8. [LeetCode] 349 Intersection of Two Arrays && 350 Intersection of Two Arrays II

    这两道题都是求两个数组之间的重复元素,因此把它们放在一起. 原题地址: 349 Intersection of Two Arrays :https://leetcode.com/problems/in ...

  9. LeetCode Javascript实现 169. Majority Element 217. Contains Duplicate(两个对象比较是否相等时,如果都指向同一个对象,a==b才是true)350. Intersection of Two Arrays II

    169. Majority Element /** * @param {number[]} nums * @return {number} */ var majorityElement = funct ...

随机推荐

  1. 行列有序矩阵求第k大元素

    问题来源:http://www.careercup.com/question?id=6335704 问题描述: Given a N*N Matrix. All rows are sorted, and ...

  2. jmeter 302请求测试

    需求,测试url:  http://test.123.com/123.action,请求该url之后会进行302跳转,判断跳转的页面是不是http://www.haha.com. 这个其实本质还是普通 ...

  3. border-radius几种写法的原理剖析

    border-radius:40px; border-radius:40px/20px; border-radius:40px 20px; border-radius:40px 20px 10px 5 ...

  4. HDFS Protocol修改流程

        相对于1.x版本的Hadoop,2.x版本的Hadoop采用了Protocol Buffer作为序列化反序列化工具,以及RPC通讯工具.这样当我们对Hadoop源码进行修改之前,就需要了解Ha ...

  5. SSH开发实践part3:hibernate继承映射

    0 大家好.上次讲了关于hibernate中双向1-N的映射配置,可以参考:http://www.cnblogs.com/souvenir/p/3784510.html 实际项目中,对象间的关系比较复 ...

  6. AA投资

    AA投资创建于2015年,总部位于北京,创始人成妙绮和王浩泽,专注于天使轮的技术创新驱动的TMT项目投资. 投资方向 AA投资是一家2015年才成立的风险投资机构,专注于种子轮.天使轮.Pre-A轮的 ...

  7. idea新建maven项目时,mvn archetype:generate 速度缓慢

    原文 idea新建maven项目时,mvn archetype:generate 速度缓慢 1 现象: 用IDEA新建maven项目,填写完各种参数,创建时,控制台卡在“[INFO] Generati ...

  8. iOS:核心动画之动画组CAAnimationGroup

    CAAnimationGroup——动画组 动画组,是CAAnimation的子类,可以保存一组动画对象,将CAAnimationGroup对象加入层后,组中所有动画对象可以同时并发运行 属性说明: ...

  9. 借助adb与gdb确认app内存缓存中是否存在用户敏感数据

    一.环境准备 1. 搭建adb调试桥 可参考文章<ADB调试桥安装(方式二)> 2. 安装调试gdb工具 可参考文章<移动设备中导入gdb调试工具> 二.测试执行 root@G ...

  10. Android 时间轴

    最近开发的app中要用到时间轴这东西,需要实现的效果如下: 想想这个东西应该可以用listview实现吧.然后最近就模拟着去写了: 首先写  listview的item的布局: listview_it ...