当前位置:网站首页>生命不息,刷题不止,简单题学习知识点
生命不息,刷题不止,简单题学习知识点
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};
}
};遇到其他集合的运算了,再补充。
边栏推荐
- SQL——左连接(Left join)、右连接(Right join)、内连接(Inner join)
- mpu9150(driverack pa简明教程)
- redis-企业级使用
- Web系统常见安全漏洞介绍及解决方案-CSRF攻击
- Curl 命令使用
- Windows系统Mysql8版本的安装教程
- A Method for Ensuring Data Consistency of Multi-Party Subsystems
- [Part 1 of Cloud Native Monitoring Series] A detailed explanation of Prometheus monitoring system
- Sql optimization summary!detailed!(Required for the latest interview in 2021)
- Three ways of single sign-on
猜你喜欢
随机推荐
Web系统常见安全漏洞介绍及解决方案-XSS攻击
半个月时间把MySQL重新巩固了一遍,梳理了一篇几万字 “超硬核” 文章!
Three ways of single sign-on
Burndown chart of project management tools: Dynamic assessment of team work ability
【云原生监控系列第一篇】一文详解Prometheus普罗米修斯监控系统(山前前后各有风景,有风无风都很自由)
面试、工作中常用sql大全(建议收藏备用)
【LeetCode】387. 字符串中的第一个唯一字符
一、excel转pdf格式jacob.jar
In half a month, MySQL has been consolidated again, and a tens of thousands of words "super hard core" article has been sorted out!
一种用于保证多方子系统数据一致性的方法
解决报错TypeError:unsupported operand type(s) for +: ‘NoneType‘ and ‘str‘
strings包详细文档+示例
浅谈Attention与Self-Attention,一起感受注意力之美
蓝牙协议栈开发板 STM32F1 跑蓝牙协议栈 –传统蓝牙搜索演示以及实现原理[通俗易懂]
【LeetCode】118.杨辉三角
新人学习小熊派华为iot介绍
keras自带数据集(横线生成器)
掌握SSR
[Part 1 of Cloud Native Monitoring Series] A detailed explanation of Prometheus monitoring system
“带薪划水”偷刷阿里老哥的面经宝典,三次挑战字节,终成正果








