LeetCode75——Day20

文章目录

一、题目

2215. Find the Difference of Two Arrays

Given two 0-indexed integer arrays nums1 and nums2, return a list answer of size 2 where:

answer[0] is a list of all distinct integers in nums1 which are not present in nums2.

answer[1] is a list of all distinct integers in nums2 which are not present in nums1.

Note that the integers in the lists may be returned in any order.

Example 1:

Input: nums1 = [1,2,3], nums2 = [2,4,6]

Output: [[1,3],[4,6]]

Explanation:

For nums1, nums1[1] = 2 is present at index 0 of nums2, whereas nums1[0] = 1 and nums1[2] = 3 are not present in nums2. Therefore, answer[0] = [1,3].

For nums2, nums2[0] = 2 is present at index 1 of nums1, whereas nums2[1] = 4 and nums2[2] = 6 are not present in nums2. Therefore, answer[1] = [4,6].

Example 2:

Input: nums1 = [1,2,3,3], nums2 = [1,1,2,2]

Output: [[3],[]]

Explanation:

For nums1, nums1[2] and nums1[3] are not present in nums2. Since nums1[2] == nums1[3], their value is only included once and answer[0] = [3].

Every integer in nums2 is present in nums1. Therefore, answer[1] = [].

Constraints:

1 <= nums1.length, nums2.length <= 1000

-1000 <= nums1[i], nums2[i] <= 1000

二、题解

cpp 复制代码
class Solution {
public:
    vector<vector<int>> findDifference(vector<int>& nums1, vector<int>& nums2) {
        int n1 = nums1.size();
        int n2 = nums2.size();
        vector<int> res1;
        vector<int> res2;
        unordered_map<int,int> map1;
        unordered_map<int,int> map2;
        for(int i = 0;i < n1;i++) map1[nums1[i]]++;
        for(int i = 0;i < n2;i++) map2[nums2[i]]++;
        for(int i = 0;i < n1;i++){
            if(map2[nums1[i]] == 0){
                res1.push_back(nums1[i]);
                map2[nums1[i]]++;
            }
        }
        for(int i = 0;i < n2;i++){
            if(map1[nums2[i]] == 0){
                res2.push_back(nums2[i]);
                map1[nums2[i]]++;
            }
        }
        vector<vector<int>> res;
        res.push_back(res1);
        res.push_back(res2);
        return res;
    }
};
相关推荐
乌萨奇也要立志学C++4 分钟前
【C++详解】STL-list模拟实现(深度剖析list迭代器,类模板未实例化取嵌套类型问题)
c++·list
努力写代码的熊大18 分钟前
链式二叉树数据结构(递归)
数据结构
yi.Ist19 分钟前
数据结构 —— 键值对 map
数据结构·算法
爱学习的小邓同学19 分钟前
数据结构 --- 队列
c语言·数据结构
s1533522 分钟前
数据结构-顺序表-猜数字
数据结构·算法·leetcode
闻缺陷则喜何志丹23 分钟前
【前缀和 BFS 并集查找】P3127 [USACO15OPEN] Trapped in the Haybales G|省选-
数据结构·c++·前缀和·宽度优先·洛谷·并集查找
Coding小公仔25 分钟前
LeetCode 8. 字符串转换整数 (atoi)
算法·leetcode·职场和发展
GEEK零零七30 分钟前
Leetcode 393. UTF-8 编码验证
算法·leetcode·职场和发展·二进制运算
DoraBigHead2 小时前
小哆啦解题记——异位词界的社交网络
算法
序属秋秋秋2 小时前
《C++初阶之内存管理》【内存分布 + operator new/delete + 定位new】
开发语言·c++·笔记·学习