[LeetCode]350. Intersection of Two Arrays II(求两个数组交集 II)

Upwork数据战争:用爬虫与AI预测垄断订单 当普通自由职业者还在“等客户发需求”时,顶尖玩家已用数据预测客户明天需要什么; 当竞争对手熬夜抢单时,你的爬虫早已自动提交提案—— 这不是科幻,而是2025年的现实! 本文将手把手教你用合法工具搭建“数据战情室”,让每个决策都有万亿级数据支撑! 阅读详情

350. Intersection of Two Arrays II

点击查看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, 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.
  • 结果可以以任何顺序。

Follow up:(进一步思考)

  • What if the given array is already sorted? How would you optimize your algorithm?
  • 如果给定数组已经排序了怎么办?你如何优化你的算法?
  • What if nums1’s size is small compared to nums2’s size? Which algorithm is better?
  • 如果nums1的规模相比,nums2尺寸小?哪种算法比较好?
  • What if elements of nums2 are stored on disk, and the memory is limited such that you cannot load all elements into the memory at once?
  • 如果元素的nums2存储在磁盘上,而记忆是有限的,你无法加载所有的元素进记忆呢?

思路1:

代码如下:

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
class Solution {
public:
    vector<int> intersect(vector<int>& nums1, vector<int>& nums2) {
        vector<int> res;
        if(nums1.empty() || nums2.empty())
            return res;
        vector<int>& NUMS1 = nums1;
        vector<int>& NUMS2 = nums2;
        sort(NUMS1.begin(), NUMS1.end());
        sort(NUMS2.begin(), NUMS2.end());

        for(int i=0,j=0; i<NUMS1.size()&&j<NUMS2.size();){
            if(NUMS1[i] == NUMS2[j]){
                res.push_back(NUMS1[i]);
                i++;j++;
                continue;
            }
            else if(NUMS1[i] > NUMS2[j]){
                j++;
            }
            else{
                i++;
            }
        }
        return res;
    }
};
int main()
{
    Solution a;
    int num1[] = {1, 2, 2, 1};
    int num2[] = {2, 2};
    vector<int> nums1(num1, num1 + sizeof(num1)/sizeof(num1[0]));
    vector<int> nums2(num2, num2 + sizeof(num2)/sizeof(num2[0]));
    vector<int> res = a.intersect(nums1, nums2);
    for(int i=0; i<res.size(); i++){
        cout << res[i] << " ";
    }
    return 0;
}

思路2:

  • 根据进一步思考的内容,相处了用hash表的方法,比较简单直接贴出代码
  • 如果是有序的可以不用hash表, 同时扫描两个数组, 找相同的.
  • 如果nums2在磁盘中, 用hash表无影响
    代码如下:
    vector<int> intersect(vector<int>& nums1, vector<int>& nums2) {
        if(nums1.size()==0 || nums2.size()==0)
                return vector<int>();
        vector<int> res;
        unordered_map<int, int> HASH;
        for(auto val1: nums1)
            HASH[val1]++;
        for(auto val2: nums2){
            if(HASH.count(val2) && HASH[val2]>0)
                res.push_back(val2);
            HASH[val2]--;
        }
         return res;
     }
  1. 如果不排序,O(mn)。
  2. 如果m和n都在合理范围内,先排序,再一个一个对比,时间复杂度O(nlgn + mlgm + m+n)。
  3. 如果m远小于n, 对n排序,m也排序(nlgn+mlgm+m+n),或者m不排序(nlgn + mn)。 这两种都差不多。也可以对m不排序,在n里做binary search(二叉查找),这样复杂度降低为nlgn+mlgn, 降很低。
  4. 如果n很大,n只能存在disk上。只能把m load到内存,然后n一个一个的读进来,和m对比,这时m可以用hash存,这样复杂度就为O(n)了。
  5. 以上结论参考点击查看
R语言 Chow Test Chow 测试用于测试不同数据集上的两个不同回归模型中的系数是否相等。 该测试通常用于计量经济学领域,使用时间序列数据来确定数据在某个时间点是否存在结构性中断。 本教程提供了如何在 R 中执行 Chow 测试的分步示例 首先,我们将创建一些假数据: #create data data <- data.frame(x = c(1, 1, 2, 3, 4, 4, 5, 5, 6, 7, 7, 8, 8, 9, 10, 10, 11, 12, 12, 13, 阅读详情

相关推荐

LSF0108 level shift设计使用及debug流程

目录 初版设计 测试出现的问题1 解决问题(尝试1) 测试出现的问题2 解决问题2-1 解决问题2-2 单Port测试结果 所有Port均连接测试结果 初版设计 此电路的应用场景在于将LCD屏幕连接到FPGA上,进而通过FPGA中设计的硬件电路来驱动LCD屏幕,验证数字硬件电路设计的正确性。因此需要一个转接板位于FPGA和LCD屏幕之间,将二者连接起来。 转接板的硬件电路设计主要存在以下需求: 实现3.3V和1.8V之间的同时双向逻辑转换(LCD屏幕电平为3.3V,FPGA电平为1.

moshanghongfeng的博客 7573

leetcode::两个数组的交集

给定两个数组 nums1 和 nums2 ,返回它们的交集输出结果中的每个元素一定是 唯一 的。我们可以不考虑输出结果的顺序。输入:nums1 = [4,9,5], nums2 = [9,4,9,8,4]输入:nums1 = [1,2,2,1], nums2 = [2,2]定义两个set,从头开始依次比较,以其中一个set为基准。解释:[4,9] 也是可通过的。

Raymood_的博客 425

10分钟上手YOLOv11:最强姿态估计模型来了,代码开源,效果炸裂!

【10分钟玩转YOLOv11实时姿态估计】🚀 YOLOv11作为YOLO家族最新成员,在速度、精度和易用性上全面升级,尤其适用于姿态估计等复杂视觉任务。本文带您快速掌握: 1️⃣ 核心概念:姿态估计即识别人体17个关键点(如关节部位)并连接成数字骨架,是智能健身、体感游戏等应用的基础。 2️⃣ 技术优势: 超轻量级(最小仅2.9M参数) 极速推理(GPU仅1.7ms) 高精度(COCO mAP@50-95达50%) 3️⃣ 三步实操: ① 安装:pip install ultralytics ② 准备人物

weixin_41246802的博客 1795

LeetCode两个数组的交集||

【容易理解】LeetCode找出两个数组的交集元素

Atom的博客 562

Linux vcan虚拟CAN接口配置与实战:从内核模块到自动化测试

CAN总线是嵌入式与汽车电子领域的基础通信协议,SocketCAN作为Linux内核原生支持,提供了标准化的CAN接口抽象。vcan虚拟CAN接口基于SocketCAN实现,通过纯软件模拟CAN网络设备,无需物理硬件即可完成报文收发与协议验证。其技术价值在于降低开发测试成本,支持多节点通信模拟、压力测试与CI集成。典型应用场景包括CANopen、J1939、UDS诊断协议开发,以及自动化回归测试。本文详细介绍了vcan内核模块加载、接口创建、参数配置、can-utils工具集使用及Python脚本通信,并涵

weixin_42709044的博客 207

微信聊天记录丢失原因与恢复方案详解

SQLite数据库作为移动应用常见的本地存储方案,其加密机制和存储结构直接影响数据安全性。以微信为例,聊天记录存储在EnMicroMsg.db等SQLite文件中,采用AES加密保护。当手机清理工具误删这些文件时,会导致特定时间段记录丢失。理解数据恢复原理需要掌握文件系统删除机制和闪存特性,其中关键点包括TRIM指令的影响和数据块覆盖风险。通过专业工具如DiskDigger或iMyFone D-Back,配合ADB调试和SQLite数据重组技术,可在黄金时间窗口内恢复记录。日常维护建议结合自动化备份方案和存

weixin_32612263的博客 388

(哈希表 ) 349. 两个数组的交集 ——【Leetcode每日一题】

349. 两个数组的交集 给定两个数组 nums1 和 nums2 ,返回 它们的交集 。输出结果中的每个元素一定是 唯一 的。我们可以 不考虑输出结果的顺序 。

weixin_43412762的博客 1398

电源完整性仿真流程梳理

可根据仿真流程导图,一步一步的去设置完成仿真。仿真的关键,在于对电路回路的理解以及所放置的电压源与电流的合理性,对于去耦电容的选择,则需要根据谐振频率计算,从而选取有效滤波的容值。

csy17729158342的博客 3450

#LeetCode刷题——350. 两个数组的交集 II

难度:easy 1、题目介绍 2、思路分析 第一种方法:双指针法 先对俩个数组进行排序,使用俩个指针 i 和 j 不停遍历nums1和nums2,比较俩个元素的值,如果相等就增加到结果集中,如果 nums1[ i ] < nums2 [ j ] ,将 i 后移一位继续比较,如果 nums1[ i ] > nums2 [ j ] , 将 j 后移一位。最终将结果集转换成数组 代码: //第一种方法:双指针法 public static int[]...

YZG ' BLOG 640

Python 爬虫实战:爬取 BOSS 直聘职位,实现岗位要求关键词提取

摘要:本文基于BOSS直聘数据分析师岗位信息,采用Python爬虫技术获取职位数据,通过文本分析和可视化方法研究岗位技能需求特征。研究首先设计爬虫程序获取职位基本信息和岗位要求文本,经数据清洗后运用jieba分词和词频统计提取关键词。结果显示SQL、Python为最核心技能,不同级别岗位需求差异明显:初级重基础数据处理,高级需建模和管理能力;行业间亦存在差异,互联网关注用户增长,金融侧重风控。研究为求职者技能提升提供了数据支持,同时展示了从非结构化文本中挖掘有价值信息的完整流程。

2503_91057718的博客 1775

QGIS之二十六pbf转osm转shp

QGIS之二十六pbf转osm转shp

yilvyangguang520的博客 7526

两个数组的交集 II

给你两个整数数组 nums1 和 nums2 ,请你以数组形式返回两数组的交集。返回结果中每个元素出现的次数,应与元素在两个数组中都出现的次数一致(如果出现次数不一致,则考虑取较小值)。可以不考虑输出结果的顺序。 示例 1: 输入:nums1 = [1,2,2,1], nums2 = [2,2] 输出:[2,2] 示例 2: 输入:nums1 = [4,9,5], nums2 = [9,4,9,8,4] 输出:[4,9] 提示: 1 <= nums1.length, nums2.len.

LEO的博客 342

精雕软件(JDpaint) 5.5 绿色版

精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版

[leetcode350]Intersection of Two Arrays II求数组交集

List<Integer> res = new ArrayList<>(); Arrays.sort(nums1); Arrays.sort(nums2); int i1 = 0; int i2 = 0; while (i1<nums1.length&&am...

weixin_30938149的博客 72

RU DOS版、EFI版32bit/64bit BIOS常用工具

DOS版、EFI版的RU工具,可用来查看IO、MEM、PCI、SMBIOS等资源。亲测在EFI SHELL下可用。

LinePhone3.2.7(android端最新版本) sip客户端

SIP客户端For android:LinePhone3.2.7(最新版本),官网地址:http://www.linphone.org/

Leetcode: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 many times as it

Genius 259

leetcode-350 Intersection of Two Arrays II 求两个数组的交集 面试见到了吗?

**Question:** Given two arrays, write a function to compute their intersection. **Example:** nums1 = [1, 2, 2, 1], nums2 = [2, 2], return [2, 2]. @Test public void test() { int[] nums1

beloveddarling的博客 517

求两个整形数组的交集c语言,leetcode-350-Intersection of Two Arrays II(求两个数组的交集)...

题目描述:Given two arrays, write a function to compute their intersection.Example:Givennums1=[1, 2, 2, 1],nums2=[2, 2], return[2, 2].Note:Each element in the result should appear as many times as i...

weixin_39628384的博客 181

注塑车间WiFi数据采集稳定性设计与MES对接实践

工业物联网中,无线数据采集常面临信号干扰与链路抖动问题。其原理在于通过网关本地缓存与断点续传机制,结合WiFi信道规划与参数调优,保障数据完整性与实时性。技术价值在于将不确定性收敛至网关侧,实现高可靠传输。典型应用于注塑车间等强干扰环境,通过MQTT协议与MES系统对接,确保生产数据不丢、不乱序。本文聚焦注塑机联网场景,详解WiFi稳定性设计、网关选型与现场调试,助力工业数据采集落地。

weixin_34293246的博客 368

LeetCode //C - 350. Intersection of Two Arrays II

【代码】LeetCode //C - 350. Intersection of Two Arrays II。

Made in Code 1495

Leetcode 350. Intersection of Two Arrays II 数组交集2 解题报告

1 解题思想这道题和前两天的那个是一个系列的,只是那一个输出不能重复,而这一题重复了多少个,就输出多少个,所以先看下这个: Leetcode 349. Intersection of Two Arrays 解题报告 Python Java 做法,在Java上,就是把原来的HashSet转变成HashMap用来计数就可以了,剩下就很简单了2 原题Given two arrays, write a f

MebiuW的专栏 4642

LeetCode刷题:349. Intersection of Two Arrays&350. Intersection of Two Arrays II

LeetCode刷题:349. Intersection of Two Arrays&350. Intersection of Two Arrays II 原题链接: 349.Intersection of Two Arrayshttps://leetcode.com/problems/intersection-of-two-arrays/ 350.Intersection o...

梅森上校的博客 业精于勤荒于嬉,形成于思毁于随。 316

Leetcode日练笔记11 #350 #167 Intersection of Two Arrays II & Two Sum II - Input array is sorted

#350 Intersection of Two Arrays II Given two integer arraysnums1andnums2, returnan array of their intersection. Each element in the result must appear as many times as it shows in both arrays and you may return the result inany order. 解题思路: 还是用回之...

Bittersweet_t的博客 1743

【LeetCode】350. Intersection of Two Arrays II 两个数组的交集 II(Easy)(JAVA)

【LeetCode】350. Intersection of Two Arrays II 两个数组的交集 II(Easy)(JAVA) 题目地址: https://leetcode.com/problems/intersection-of-two-arrays-ii/ 题目描述: Given two arrays, write a function to compute their intersection. Example 1: Input: nums1 = [1,2,2,1], nums2 = [2,2

qq_16927853的博客 213

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

题目链接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], ret

Coder_Orz的博客 4510

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

题目描述 【leetcode】350. 两个数组的交集 II( Intersection of Two Arrays II ) 给定两个数组,编写一个函数来计算它们的交集。 示例 1: 输入: nums1 = [1,2,2,1], nums2 = [2,2] 输出: [2,2] 示例 2: 输入: nums1 = [4,9,5], nums2 = [9,4,9,8,4] 输出: [4,9] ...

Liber-coder的博客 232

Leetcode :Intersection of Two Arrays 两个数组的交集

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

浅时光_XinLee 443

LeetCode 350. 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], retu

James Pan 1482

[leetcode] 350. 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], ret

小榕流光的专栏 2580
上一篇: [LeetCode]206. Reverse Linked List(反转单链表)
下一篇: [LeetCode]551. Student Attendance Record I(根据学生出勤记录判断是否应获奖金)
whl_program
博客等级 码龄11年 101粉丝 279原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值