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

Example 1:

Input: nums1 = [1,2,2,1], nums2 = [2,2]
Output: [2,2]

Example 2:

Input: nums1 = [4,9,5], nums2 = [9,4,9,8,4]
Output: [4,9]

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]
"""
n1=len(nums1)
n2=len(nums2) if n2>n1:
temp=nums1
nums1=nums2
nums2=temp
te=n1
n1=n2
n2=te ans=[] for i in range(n2):
if nums2[i] in nums1:
ans.append(nums2[i])
nums1.remove(nums2[i])
return ans

  

[LeetCode&Python] Problem 350. Intersection of Two Arrays II的更多相关文章

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

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

  2. [LeetCode&Python] Problem 349. Intersection of Two Arrays

    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

    problem 350. Intersection of Two Arrays II 不是特别明白这道题的意思,例子不够说明问题: 是按顺序把相同的元素保存下来,还是排序,但是第二个例子没有重复... ...

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

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

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

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

  6. 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 ...

  7. [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 ...

  8. [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 ...

  9. 【一天一道LeetCode】#350. Intersection of Two Arrays II

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given t ...

随机推荐

  1. dubbo 负载均衡

    在系统中可以启动多个 provider 实例,consumer 发起远程调用时,根据指定的负载均衡算法选择一个 provider. 在本机配置多个 provider,使用不同的端口: <dubb ...

  2. Qt_阴影效果

    一.控件阴影效果 为子部件添加阴影比较简单,使用如下方式: QGraphicsDropShadowEffect *shadow_effect = new QGraphicsDropShadowEffe ...

  3. 模块之 logging, shelve, sys 模块

    一. logging模块 用来记录日志,日志:记录某个时间点发生了什么事 日志作用:程序调试 了解软件程序的运行情况,是否正常 软件程序运行故障分析与问题定位 还可用来做用户行为分析 日志等级:在不改 ...

  4. Easyui的datagrid的行编辑器Editor中添加事件(修改某个单元格带出其他单元格的值)

    项目中有个datagrid需要编辑行时,用到Editor的属性,那么如何添加一个事件 问题:同一个编辑行中的某个单元格值改变时,修改其他单元格的值 页面用到的datagrid <table id ...

  5. prototype:构造函数的真相、原型链

    函数不是构造函数,但是当且仅当使用 new 时,函数调用会变成 ‘构造函数调用’.那么对 ’构造函数‘ 最准确的解释是:所有带 new 的函数调用. Nothing 只是一个普通的函数,但使用 new ...

  6. inode占用100%时硬盘无法写入文件故障处理

    故障现象: 分区无法写入文件. 故障分析: 执行df -h命令发现空间占用不到50%,执行df -hi,发现某分区IUse%值为99%,说明innode已经用完,应该是某些目录下存在大量的小文件导致. ...

  7. caffe操作技巧

    查看网络结构: (1)利用caffe自带的Python,可以将*.prototxt保存为一张图片, sudo  python python/draw_net.py  *.prototxt  *.png ...

  8. C++面向对象实现封装线程池

    body, table{font-family: 微软雅黑; font-size: 13.5pt} table{border-collapse: collapse; border: solid gra ...

  9. Bluedroid: 蓝牙协议栈源码剖析

    一. 基础知识介绍 1.缩略语 BTIF: Bluetooth Interface BTU : Bluetooth Upper Layer BTM: Bluetooth Manager BTE: Bl ...

  10. PE文件 03 重定位表

    0x01  重定位表结构   重定位表是由数据目录表中的第六个成员指出的: typedef struct _IMAGE_DATA_DIRECTORY { DWORD VirtualAddress; D ...