当前位置:网站首页>生命不息,刷题不止,简单题学习知识点
生命不息,刷题不止,简单题学习知识点
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};
}
};
遇到其他集合的运算了,再补充。
边栏推荐
猜你喜欢
随机推荐
mpu9150(driverack pa简明教程)
【LeetCode】21. 合并两个有序链表
细讲DDD领域驱动设计
keras自带数据集(横线生成器)
[Part 1 of Cloud Native Monitoring Series] A detailed explanation of Prometheus monitoring system
解决报错TypeError:unsupported operand type(s) for +: ‘NoneType‘ and ‘str‘
sql力扣刷题六
Experience innovation and iteration through the development of a lucky draw applet
7 days to learn Go, Go structure + Go range to learn
7 天能找到 Go 工作吗?学学 Go 数组和指针试试
redis-enterprise use
WSL2安装.NET 6
SQLServer2019 installation (Windows)
[Go Affair] See through Go's collections and slices at a glance
2022/7/30
7 天找个 Go 工作,Gopher 要学的条件语句,循环语句 ,第3篇
Windows安装mysql详细步骤(通俗易懂,简单上手)
SQL如何从字符串截取指定字符(LEFT、MID、RIGHT三大函数)
Curl 命令使用
Dart Log tool class