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. 常系数线性递推的第n项及前n项和 (Fibonacci数列,矩阵)

      (一)Fibonacci数列f[n]=f[n-1]+f[n-2],f[1]=f[2]=1的第n项的快速求法(不考虑高精度). 解法: 考虑1×2的矩阵[f[n-2],f[n-1]].根据fibon ...

  2. 微信小程序 折叠效果

    <view class='help'> <view class='help_item'> <view class='title' data-index='1' catch ...

  3. Linux打包、压缩与解压详解

    介绍:在Windows下最常见的压缩文件就只有两种,另一个是.rar,它有.gz..tar.gz.tgz.bz2..Z..tar等众多的压缩文件名,本文就来对这些常见的压缩文件进行总结,在具体总结各类 ...

  4. 前端开发笔记(1)html基础

    HTML介绍 HTML是HyperTextMarkupLanguage超文本标记语言的缩写 HTML是标记语意的语言 编辑器 任何纯文本编辑器都能够编辑html,比如记事本,editplus,note ...

  5. Atitit.web的自动化操作与信息抓取 attilax总结

    Atitit.web的自动化操作与信息抓取 attilax总结 1. Web操作自动化工具,可以简单的划分为2大派系: 1.录制回放 2.手工编写0 U' z; D! s2 d/ Q! ^1 2. 常 ...

  6. smarty详细使用教程(韩顺平smarty模板技术笔记)

    MVC是一种开发模式,强调数据的输入.处理.显示是强制分离的 Smarty使用教程1.如何配置我们的smarty解压后把libs文件夹放在网站第一级目录下,然后创建两个文件夹templates 存放模 ...

  7. Gson解析复杂JSON字符串的两种方式

    JSON解析可以使用的库: JSONObject(源自Android官方). Gson(源自Google). Jackson(第三方开源库). FastJSON(第三方开源库). 本文例子使用Goog ...

  8. 重学C语言---03数据和C

    1.数据的必要性.数据使我们生活中不可缺少的东西,程序也是如次,离不开数据.将文字.图片和单词等输入到算计,将其展现出来或者做一系列操作等. 2.实例程序. /*rhodium.c--用金属铑衡量体重 ...

  9. sql Server插不进数据,以及Id自增的教程及注意事项

    参考于:https://jingyan.baidu.com/article/fec4bce244f902f2608d8b7a.html 使用SQL Server 2014 数据库做web的项目出现错误 ...

  10. Azure 中虚拟机的计划内维护

    Azure 定期执行更新,以提高虚拟机的主机基础结构的可靠性.性能及安全性. 此类更新包括修补宿主环境(例如操作系统.虚拟机监控程序以及主机上部署的各种代理)中的软件组件.升级网络组件以及硬件解除授权 ...