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. python基础训练题2-元组,字典

    1,判断值在元组中 >>> a = ( 1, 2, 3, 4, 10 ) >>> 10 in a True >>> ' in a False 2, ...

  2. 一个挺好用的生成GIF格式图片的小程序

    import os import re import imageio pic_list = os.listdir() pic_type = ['png', 'jpg', 'jpeg', 'bmp'] ...

  3. PDO异常处理

    PDO提供了三种处理错误的方式 PDO::ERRMODE_SILENT:静默模式(默认) PDO::ERRMODE_WARNING:警告模式 PDO::ERRMODE_EXCEPTION:异常模式 示 ...

  4. element-ui input组件源码分析整理笔记(六)

    input 输入框组件 源码: <template> <div :class="[ type === 'textarea' ? 'el-textarea' : 'el-in ...

  5. 火狐浏览器sqlite插件

    https://addons.mozilla.org/zh-cn/firefox/addon/sqlite-manager/

  6. python数据类型之间的转换

    1,字符串转整型,前提条件是该字符串为纯数字. a = '1' a = int(a) 2,整型转字符串 a= 1 a = str(a) 3,整型转浮点型 a = 1 a = float(a) 4,浮点 ...

  7. 控制台输出 mybatis 中的sql语句

    控制台输出 mybatis 中的sql语句 在 log4j.xml 文件中 增加如下配置 <!-- mybatis 输出的sql,DEBUG级别 --> <logger name=& ...

  8. 在 Azure 中管理 Windows 虚拟机的可用性

    了解如何设置和管理多个虚拟机,以确保 Azure 中 Windows 应用程序的高可用性. 也可以管理 Linux 虚拟机的可用性. Note Azure 具有用于创建和处理资源的两个不同的部署模型: ...

  9. jmeter之数据库相关

    一.JDBC Connection Configuration 1.Variable Name Bound to Pool-Variable Name:连接池名称, JDBC Request通过此名称 ...

  10. 转-python异步IO-asyncio

    原文连接 http://blog.chinaunix.net/uid-190176-id-4223282.html 前言 异步操作在计算机软硬件体系中是一个普遍概念,根源在于参与协作的各实体处理速度上 ...