当前位置:网站首页>生命不息,刷题不止,简单题学习知识点
生命不息,刷题不止,简单题学习知识点
2022-07-31 11:01:00 【风一样的航哥】
难度简单7收藏分享切换为英文接收动态反馈
给你两个下标从 0
开始的整数数组 nums1
和 nums2
,请你返回一个长度为 2
的列表 answer
,其中:
answer[0]
是nums1
中所有 不 存在于nums2
中的 不同 整数组成的列表。answer[1]
是nums2
中所有 不 存在于nums1
中的 不同 整数组成的列表。
注意:列表中的整数可以按 任意 顺序返回。
示例 1:
输入:nums1 = [1,2,3], nums2 = [2,4,6] 输出:[[1,3],[4,6]] 解释: 对于 nums1 ,nums1[1] = 2 出现在 nums2 中下标 0 处,然而 nums1[0] = 1 和 nums1[2] = 3 没有出现在 nums2 中。因此,answer[0] = [1,3]。 对于 nums2 ,nums2[0] = 2 出现在 nums1 中下标 1 处,然而 nums2[1] = 4 和 nums2[2] = 6 没有出现在 nums2 中。因此,answer[1] = [4,6]。
示例 2:
输入:nums1 = [1,2,3,3], nums2 = [1,1,2,2] 输出:[[3],[]] 解释: 对于 nums1 ,nums1[2] 和 nums1[3] 没有出现在 nums2 中。由于 nums1[2] == nums1[3] ,二者的值只需要在 answer[0] 中出现一次,故 answer[0] = [3]。 nums2 中的每个整数都在 nums1 中出现,因此,answer[1] = [] 。
提示:
1 <= nums1.length, nums2.length <= 1000
-1000 <= nums1[i], nums2[i] <= 1000
通过次数10,795提交次数16,126
题解:题目说了这么多,其实就是两个集合的差。这个C++已经实现了,直接调用即可。
class Solution {
public:
vector<vector<int>> findDifference(vector<int>& nums1, vector<int>& nums2) {
set<int> s1(nums1.begin(), nums1.end());
set<int> s2(nums2.begin(), nums2.end());
vector<int> v1, v2;
set_difference(s1.begin(), s1.end(), s2.begin(), s2.end(), back_inserter(v1));
set_difference(s2.begin(), s2.end(), s1.begin(), s1.end(), back_inserter(v2));
return {v1, v2};
}
};
遇到其他集合的运算了,再补充。
边栏推荐
猜你喜欢
“chmod 777-R 文件名”什么意思?
浅谈Attention与Self-Attention,一起感受注意力之美
如何优雅的停止一个线程?
Sql optimization summary!detailed!(Required for the latest interview in 2021)
Single sign-on principle and implementation
一、excel转pdf格式jacob.jar
“带薪划水”偷刷阿里老哥的面经宝典,三次挑战字节,终成正果
准确率(Accuracy)、精度(Precision)、召回率(Recall)和 mAP 的图解
Intranet Penetration Learning (IV) Domain Lateral Movement - SMB and WMI Service Utilization
Web系统常见安全漏洞介绍及解决方案-CSRF攻击
随机推荐
Summary of three methods for SQL deduplication
半个月时间把MySQL重新巩固了一遍,梳理了一篇几万字 “超硬核” 文章!
odoo14 | 附件上传功能及实际使用
3D激光SLAM:LeGO-LOAM论文解读---点云分割部分
SQL学习笔记——REGEXP运算符
SQL——左连接(Left join)、右连接(Right join)、内连接(Inner join)
SQL力扣刷题七
[Part 1 of Cloud Native Monitoring Series] A detailed explanation of Prometheus monitoring system
【LeetCode】141.环形链表
Usage of JOIN in MySQL
【云原生监控系列第一篇】一文详解Prometheus普罗米修斯监控系统(山前前后各有风景,有风无风都很自由)
KVM virtualization job
【LeetCode】383.赎金信
Inversion problem - key point
《MySQL高级篇》四、索引的存储结构
NowCoderTOP23-27 Binary tree traversal - continuous update ing
「MySQL」- 基础增删改查
Threading(in thread main)
How SQL intercepts specified characters from strings (three functions of LEFT, MID, RIGHT)
SQL去重的三种方法汇总