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. 【JavaFx教程】第四部分:CSS 样式

    第4部分主题 CSS样式表 添加应用程序图标 CSS样式表 在JavaFX中,你能使用层叠样式表修饰你的用户接口.这非常好!自定义Java应用界面从来不是件简单的事情. 在本教程中,我们将创建一个*D ...

  2. javascript如何获取URL参数的值

    function getUrlParameter(strParame){ var args = new Object( ); var query = location.search.substring ...

  3. 小程序通过用户授权获取手机号之getPhoneNumber

    小程序有一个获取用户很便捷的api,就是通过getPhoneNumber获取用户的已经绑定微信的手机号码.有一点要大家注意,现在微信和注重用户体验,有些方法都是需要用户主动去触发才能调用的,比如get ...

  4. JS 正则截取字符串

    1.js截取两个字符串之间的内容: varstr = "aaabbbcccdddeeefff"; str = str.match(/aaa(\S*)fff/)[1]; alert( ...

  5. jQuery中hover方法和toggle方法使用指南

    jQuery提供一些方法(如:toggle)将两种事件效果合并到一起,比如:mouseover.mouseout:keyup.keydown等 1.hover函数 hover(over,out)一个模 ...

  6. php判断是不是移动设备

    <?php function isMobile() {     // 如果有HTTP_X_WAP_PROFILE则一定是移动设备     if (isset ($_SERVER['HTTP_X_ ...

  7. CAT3 SAP tcode - Time Sheet: Display Times

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

  8. Pycharm代码补齐功能中的图标的意思

    分清楚图标的意思就能更好的使用对应的方法.类,避免错误使用括号 PS:博主老是给属性方法加上括号   代表方法: 红色的m.f,   代表类变量: 黄色的f     之前遇到个属性方法: 好像是p,无 ...

  9. Windows桌面.exe程序安装、卸载、升级测试用例

    一.安装 1) 系统:XP.win 7.win 8.win 10 2)安全类型软件:360杀毒.360安全卫士.金山毒霸.百度杀毒.腾讯电脑管家等. 3)同类型软件兼容 4)用户名称:中文用户.英文用 ...

  10. CSS 小结笔记之滑动门技术

    所谓的滑动门技术,就是指盒子背景能够自动拉伸以适应不同长度的文本.即当文字增多时,背景看起来也会变长. 大多数应用于导航栏之中,如微信导航栏: 具体实现方法如下: 1.首先每一块文本内容是由a标签与s ...