350. Intersection of Two Arrays II [easy] (Python)

【STABLE_BASELINE3】强化学习算法实战:模型的保存,读取,再训练 STABLE-BASELINE3,模型读取,再训练:有时候需要在已经训练好的模型基础上进行再次训练。 阅读详情

题目链接

https://leetcode.com/problems/intersection-of-two-arrays-ii/

题目原文

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.

题目翻译

给定两个数组,写一个函数计算它们的交。
比如:给定 nums1 = [1, 2, 2, 1], nums2 = [2, 2],返回 [2, 2]。
注意:结果中每个元素出现的次数应该与它同时出现在两个数组中的次数相同;结果的顺序没有要求。

思路方法

思路一

暴力查找法,遍历第一个列表的每个元素,并在第二个列表中查找该元素是否出现,若出现且结果集中没有,则加入到结果集中。

代码

class Solution(object):
    def intersect(self, nums1, nums2):
        """
        :type nums1: List[int]
        :type nums2: List[int]
        :rtype: List[int]
        """
        res = []
        for k in nums1:
            index = -1
            for j in xrange(0, len(nums2)):
                if nums2[j] == k:
                    index = j
                    break
            if index != -1:
                res.append(k)
                del nums2[index]

        return res

思路二

类似思路一,为了提高效率,先对第二个列表先排序,每次检查元素是否出现时用二分搜索。

代码

class Solution(object):
    def intersect(self, nums1, nums2):
        """
        :type nums1: List[int]
        :type nums2: List[int]
        :rtype: List[int]
        """
        res = []
        nums2.sort()
        for k in nums1:
            flag, j = self.binarySearch(nums2, k)
            if flag:
                res.append(k)
                del nums2[j]
        return res

    def binarySearch(self, nums, num):
        left = 0
        right = len(nums) - 1
        while left <= right:
            mid = (left + right) / 2
            if nums[mid] == num:
                return True, mid
            if nums[mid] < num:
                left = mid + 1
            else:
                right = mid - 1
        return False, 0

思路三

用字典统计第一个列表都出现了哪些数及出现的次数,然后顺序遍历第二个列表,发现同时出现的数则加入到结果列表中,同时将第一个列表中相应的出现次数减一。

代码

class Solution(object):
    def intersect(self, nums1, nums2):
        """
        :type nums1: List[int]
        :type nums2: List[int]
        :rtype: List[int]
        """
        res = []
        map = {}
        for i in nums1:
            map[i] = map[i]+1 if i in map else 1
        for j in nums2:
            if j in map and map[j] > 0:
                res.append(j)
                map[j] -= 1

        return res

思路四

类似思路三的思想,不过是将两个数组都统计成dict,再计算交集。这时可以利用Python的collections模块的Counter来帮助统计,实现一行代码AC。

代码

class Solution(object):
    def intersect(self, nums1, nums2):
        """
        :type nums1: List[int]
        :type nums2: List[int]
        :rtype: List[int]
        """
        return list((collections.Counter(nums1) & collections.Counter(nums2)).elements())

思路五

先将两个数组排序,再维持两个指针分别扫描两个数组,寻找共同的元素。

代码

class Solution(object):
    def intersect(self, nums1, nums2):
        """
        :type nums1: List[int]
        :type nums2: List[int]
        :rtype: List[int]
        """
        res = []
        nums1.sort()
        nums2.sort()
        i = j = 0
        while i < len(nums1) and j < len(nums2):
            if nums1[i] == nums2[j]:
                res.append(nums1[i])
                i += 1
                j += 1
            elif nums1[i] < nums2[j]:
                i += 1
            elif nums1[i] > nums2[j]:
                j += 1

        return res

PS: 新手刷LeetCode,新手写博客,写错了或者写的不清楚还请帮忙指出,谢谢!
转载请注明:http://blog.csdn.net/coder_orz/article/details/51496278

手把手教你用Pico4UE搭建100平米VR大空间:从设备检查、系统升级到地图绘制全流程 本文详细介绍了使用Pico4UE搭建100平米VR大空间的完整流程,包括设备检查、系统升级、地图绘制和性能优化等关键步骤。通过实战案例和专业技术建议,帮助用户高效部署企业级VR解决方案,适用于展厅漫游、工业仿真等多种场景。 阅读详情

相关推荐

AutoCAD 2025在Win10/Win11上的官方安装、配置与问题排查全指南

对于需要在 Windows 11 或 Windows 10 系统上部署 AutoCAD 2025 的设计师、工程师和 IT 支持人员来说,一个稳定、合规的安装流程是高效工作的基石。网络上充斥着各种所谓的“免费下载”和“一键安装”教程,但这些资源往往来源不明,可能捆绑恶意软件、导致软件崩溃,甚至引发严重的许可合规风险。本文旨在提供一个清晰、安全、基于官方最佳实践的 AutoCAD 2025 安装与配置指南,重点解决在 Windows 11 和 Windows 10 系统上部署时可能遇到的兼容性问题、安装失败以

weixin_34396103的博客 458

349. Intersection of Two Arrays

Given two arrays, write a function to compute their intersection. Example: Given nums1 = [1, 2, 2, 1], nums2 = [2, 2], return [2]. Note: Each element in the result must be unique.The result

erinapple的博客 213

树莓派5安装ROS2小白指南避坑攻略

手把手教你如何在树莓派5上顺利安装ROS2,避开常见问题与陷阱,提升配置效率。针对树莓派5安装ros2过程中的依赖冲突、源配置错误等高频难题提供解决方案,适合新手快速上手。

weixin_35754962的博客 1009

leetcode【数组】-----350. Intersection of Two Arrays II(两个数组的交集 II)

1、题目描述 2、分析 给定两个数组,编写一个函数来计算它们的交集。 这道题和上道题很像,但是相比较而言,上道题只需要输出重复的一个元素就行,所以上道题可以采用set。这道题可以将数组nums1存入一个哈希表,数组的值和这个值出现的次数构成一组对应元素,然后遍历数组2,如果哈希表中这个值为0代表没有这个元素,如果不为0则插入到结果中,并且将哈希表的值出现次...

一个人的学习和碎碎念 169

CGAL-5.6的安装与编译(Win10+vs2022+CGAL-5.6+Boost1.82.0)

CGAL是Computational Geometry Algorithms Library(计算几何算法库)的缩写,C++的高效算法库。CGAL只是一个头文件库(从5.0版本开始),这意味着CGAL无需编译,只需安装好CGAL的依赖项即可。CGAL5.6。

baidu_34931359的博客 3908

用 Custom Entity 打造 RAP Value Help:从能跑到好用的深度踩坑笔记

本文介绍了在RAP项目中实现Value Help(Fiori输入帮助)的完整流程。通过Custom Entity方式,开发者可以灵活构建不依赖数据库实体的值帮助功能,但需手动实现CDS原本提供的过滤、排序、分页等能力。文章以一个典型场景为例,详细说明了如何为Partner维护界面的City字段创建可搜索的值帮助,包括创建查询实现类、绑定CDS Custom Entity、服务发布等关键步骤,并特别指出了处理排序分页请求等常见问题。最后强调在ABAP Cloud环境下需要自行实现查询能力,而非依赖传统工具类。

2007 年 ~ 2025 年,深耕 SAP 技术 18 年 148

Intersection of Two Arrays II两个数组交集(重要!)

https://leetcode.com/problems/intersection-of-two-arrays-ii/ Given two arrays, write a function to compute their intersection. Example: Given nums1 = [1, 2, 2, 1], nums2 = [2, 2], retur

知之可否 2364

Claude桌面端安装失败?Retrying无限重试终极解决方案(亲测有效)

Claude桌面端安装卡在Retrying并非网络问题,而是安装器下载源异常导致。本文提供已验证方案:一条命令即可解决问题🚀

XiaoqiangClub的博客 2376

350. Intersection of Two Arrays II(python)

class Solution(object):      def intersect(self, nums1, nums2):          """         :type nums1: List[int]         :type nums2: List[int]         :rtype: List[int]         """          res = []      ...

BUPT-WT的博客 270

【Unity】【Photon】Photon Fusion2 的核心Unity组件 NetworkRunner 学习笔记

本文是学习Photon Fusion2 的核心Unity组件 NetworkRunner的学习笔记。

O青草O的博客 Unity和游戏制作知识分享 1276

POSTMAN从入门到精通系列(十九):Cookie

饼干 Postman的本机应用程序提供MANAGE COOKIES模式,允许您编辑与每个域关联的Cookie。如果您想使用Postman Interceptor捕获cookie,请参阅捕获cookie部分。 前往cookie Manage 要打开MANAGE COOKIES模式,请单击“发送”按钮下的“Cookie”链接。 这将打开MANAGE COOKIES模式,并显示域列表和与...

u012014531的博客 1015

LeetCode--350. Intersection of Two Arrays II(两个数组的交集)Python

题目:给定两个数组,返回这两个数组的交集。解题思路:使用哈希表用来存储第一个数组中的内容。再遍历第二个数组,看该数组的数字是否在哈希表中,在则将该数字加入输出的列表中。代码(Python):class Solution(object): def intersect(self, nums1, nums2): """ :type nums1: List[int]

诚实的小小乐 691

【高级强化学习:算法、优化与泛化】第13章 扩散模型策略优化(Diffusion Policy Optimization)

\quad$ 计算均值 $\mu_t = \frac{1}{\sqrt{\alpha_t}} \left( x_t - \frac{1 - \alpha_t}{\sqrt{1 - \bar{\alpha}_t}} \tilde{\epsilon} \right)$.$\quad$ 组合得分估计: $\tilde{\epsilon} = \epsilon_{\text{unc}} + w \cdot (\epsilon_{\text{con}} - \epsilon_{\text{unc}})$.

u012133341的博客 344

【python3】leetcode 350. Intersection of Two Arrays II(easy)

350. Intersection of Two Arrays II(easy) 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: n...

momottyy的专栏 224

jre9下载 jdk9

(Java SE Runtime Environment 9.0) 9.0u175 官方最新版 【x86|x64】

C# WinForm 基于DirectShow.dll编写的程序,可边预览边保存视频,音频和图片

基于微软的DirectShow.dll的二次开发1.自动识别连接的摄像头跟麦克风且可以对其进行选择(默认选中第一个)2.可录制wmv和avi两种格式的视频并且都带有音频,avi可以录制的分辨率和帧数进行选择,wmv默认进行了淸晰度优化如不喜欢可去除代码中有相关提示3.可在预览点开之后进行拍照,也可边录制视频边拍照4.添加了视频和音频的压缩功能(酌情使用)这次二次开发发现C#对DirectShow的相关信息太少,这次做完也不敢藏私共享出来,还望大家指正

leetcode 350. intersection-of-two-arrays-ii 两个数组的交集 II python3

时间:2020-9-3 题目地址:https://leetcode-cn.com/problems/intersection-of-two-arrays-ii/ 题目难度:Easy 题目描述: 给定两个数组,编写一个函数来计算它们的交集。 示例 1: 输入:nums1 = [1,2,2,1], nums2 = [2,2] 输出:[2,2] 示例 2: 输入:nums1 = [4,9,5], nums2 = [9,4,9,8,4] 输出:[4,9] 说明: 输出结果中每个元素出现的次数,应与

isabloomingtree的博客 237

廖雪峰Python教程[完整版]

廖雪峰的python教程整理的,已经排好版了,而且还是完整的,适合小白

leetcode 350. Intersection of Two Arrays II

350. Intersection of Two Arrays II  My Submissions Question Editorial Solution Total Accepted: 4482 Total Submissions: 10704 Difficulty: Easy Given two arrays, write a function to

slam、机器人专家 986

IMSL CNL 7.0.0 (x86-64)Crack

IMSL CNL 7.0.0 (x86-64)Crack intel的算法库破解文件,这是C++版本的破解文件

leetcode 350-Intersection of Two Arrays II

难度:easy 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

fawnzou2017的博客 207

【LeetCode】350. Intersection of Two Arrays II 解题报告(Java & Python)

[LeetCode]https://leetcode.com/problems/intersection-of-two-arrays-ii/ Difficulty: Easy Question Given two arrays, write a function to compute their intersection. Example:Given nums1 = [1, 2, 2, 1],

负雪明烛 2541

Easy-题目73:350. Intersection of Two Arrays II(增补6)

题目原文: 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 t

320

Leetcode_Sort -- 350. Intersection of Two Arrays II [Easy]

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], nums...

Ling的博客 150

leetcode 350 Intersection of Two Arrays II (两个数组的交集 II) python3 多种思路,最简代码

class Solution: def intersect(self, nums1, nums2): &amp;amp;amp;quot;&amp;amp;amp;quot;&amp;amp;amp;quot; :type nums1: List[int] :type nums2: List[int] :rtype: List[int] &amp;amp;amp;quot;&a

Jasmine_dream 788

349. Intersection of Two Arrays [easy] (Python)

题目链接https://leetcode.com/problems/intersection-of-two-arrays/题目原文 Given two arrays, write a function to compute their intersection. Example: Given nums1 = [1, 2, 2, 1], nums2 = [2, 2], return

Coder_Orz的博客 3198

350. Intersection of Two Arrays II python

给定两个数组,写一个函数来计算它们的交集。注意 结果中的每个元素的出现次数应与其在两个数组中同时出现的次数一样多。Example:Given nums1 = [1, 2, 2, 1], nums2 = [2, 2], return [2, 2].class Solution(object): def intersect(self, nums1, nums2): """ ...

mario_mmh的博客 225

[python][leetcode]Intersection of Two Arrays II

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

kker的博客 535

LeetCode--No.350--Intersection of Two Arrays II

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 ma

sophia_tone2w的博客 304

leetcode 350. Intersection of Two Arrays II (python)

描述 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 m

王大呀呀的博客 307
上一篇: 349. Intersection of Two Arrays [easy] (Python)
下一篇: 235. Lowest Common Ancestor of a Binary Search Tree [easy] (Python)
coder_orz
博客等级 码龄14年 102粉丝 90原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值