Given two arrays, write a function to compute their intersection.

Example 1:

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

Example 2:

Input: nums1 = [4,9,5], nums2 = [9,4,9,8,4]
Output: [9,4]

Note:

  • Each element in the result must be unique.

  • The result can be in any order.

想法:先找出两个vector中相等的元素,放入另外一个vector中。然后去掉重复元素

注意:vector中的成员函数unique()只是去除相邻的重复元素,因而需要对vector内部的元素进行排序。但是当执行unique()函数后,去除的重复元素仍然存在,因而需要使用erase()完全去除尾部的元素。

class Solution {
public:
    vector<int> intersection(vector<int>& nums1, vector<int>& nums2) {
        vector<int> result;
         ; i < nums1.size() ; i++){
            ;j<nums2.size() ; j++){
                if(nums1.at(i) == nums2.at(j)){
                    result.push_back(nums1[i]);
                }
            }
        }
        sort(result.begin(),result.end());
    result.erase(unique(result.begin(), result.end()), result.end());

        return result;
    }
};

leetcode349—Intersection of Two Arrays的更多相关文章

  1. [leetcode349]Intersection of Two Arrays

    设计的很烂的一道题 List<Integer> res = new ArrayList<>(); // int l1 = nums1.length; // int l2 = n ...

  2. leetcode349 350 Intersection of Two Arrays & II

    """ Intersection of Two Arrays Given two arrays, write a function to compute their in ...

  3. [LeetCode] Intersection of Two Arrays II 两个数组相交之二

    Given two arrays, write a function to compute their intersection. Example:Given nums1 = [1, 2, 2, 1] ...

  4. [LeetCode] Intersection of Two Arrays 两个数组相交

    Given two arrays, write a function to compute their intersection. Example:Given nums1 = [1, 2, 2, 1] ...

  5. LeetCode Intersection of Two Arrays

    原题链接在这里:https://leetcode.com/problems/intersection-of-two-arrays/ 题目: Given two arrays, write a func ...

  6. LeetCode Intersection of Two Arrays II

    原题链接在这里:https://leetcode.com/problems/intersection-of-two-arrays-ii/ 题目: Given two arrays, write a f ...

  7. [LintCode] Intersection of Two Arrays II 两个数组相交之二

    Given two arrays, write a function to compute their intersection.Notice Each element in the result s ...

  8. [LintCode] Intersection of Two Arrays 两个数组相交

    Given two arrays, write a function to compute their intersection.Notice Each element in the result m ...

  9. Intersection of Two Arrays | & ||

    Intersection of Two Arrays Given two arrays, write a function to compute their intersection. Example ...

随机推荐

  1. POJ1611(KB2-B)

    The Suspects Time Limit: 1000MS   Memory Limit: 20000K Total Submissions: 39211   Accepted: 18981 De ...

  2. CAT3 SAP tcode - Time Sheet: Display Times

    CAT3 SAP tcode - Time Sheet: Display Times CAT3 (Time Sheet: Display Times) is a standard SAP transa ...

  3. Vue入门系列(五)Vue实例详解与生命周期

    Vue官网: https://cn.vuejs.org/v2/guide/forms.html#基础用法 [入门系列] (一)  http://www.cnblogs.com/gdsblog/p/78 ...

  4. Asp.Net Core Docker镜像更新系统从wheezy改为stretch

    之前写过一个在Asp.Net Core里调用System.Drawing.Common绘图的DEMO,部署到Docker里运行,需要更新Asp.Net Core镜像的操作系统. https://www ...

  5. Django开发笔记(一)

    Django开发笔记(一) 标签(空格分隔): Django Python 1. 创建并运行Django项目 创建开发环境 安装Django pip install django==version 执 ...

  6. 破解 jar 包之直接修改 .class 文件方式

    一.常规 JAVA 软件破解流程 先讲一下常规jar包的破解流程. 1. 快速定位.          1) 通过procmon监控相关软件,查看程序都访问了些啥.         2) 用jd-gu ...

  7. Freemarket语法

    <#--freemarker HashMap取值--> <#assign maps={"1":"张三丰","2":&quo ...

  8. Finding the source of signals on Linux with strace, auditd, or systemtap

    inux and UNIX® like operating systems commonly use signals to communicate between processes. The use ...

  9. Window10 Linux子系统挂载磁盘

    默认情况下, Linux子系统将当前winodws磁盘的盘全部挂载到/mnt/<disk_label>, 但一些新增的盘就需要手动做下了.. 官方参考文档 挂载磁盘 -- DrvFs 挂载 ...

  10. CVE-2013-2551

    目录 小白的CVE-2013-2551 分析 & 利用 0xFF 前言 0x00 环境和工具 0x01 分析POC POC 调试 0x02 利用 构造R3任意内存读写 劫持eip 利用利用 0 ...