349. Intersection of Two Arrays

转载 2016年05月30日 11:51:01

题目:

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

Example:
Given nums1 = [1, 2, 2, 1]nums2 = [2, 2], return [2].

Note:

  • Each element in the result must be unique.
  • The result can be in any order.
题意:

给定两个数组,写一个功能计算两者的交叉部分。

note:

1、结果集中的每一个元素必须唯一;

2、结果集可以是任何顺序


思路一:

将两个数组排序,之后逐个数进行对比,排除相同的元素。

代码:11ms

class Solution {
public:
    vector<int> intersection(vector<int>& nums1, vector<int>& nums2) {
        
        sort(nums1.begin(), nums1.end());
        sort(nums2.begin(), nums2.end());
        int c1 = 0;
        int c2 = 0;
        vector<int> result;
        
        while(c1<nums1.size() && c2<nums2.size()){
            if(nums1[c1]==nums2[c2]){
                if(!result.size() || result.back()!=nums1[c1]){
                    result.push_back(nums1[c1]);
                }
                c1++;
                c2++;
            }else if(nums1[c1]>nums2[c2]){
                c2++;
            }else{
                c1++;
            }
        }
        
        return result;
    }
};
思路二:

借助set集合的元素不相同型完成相同元素的排除,之后直接比较两个数组之中的元素即可。

代码:8ms

class Solution {
public:
    vector<int> intersection(vector<int>& nums1, vector<int>& nums2) {
        
        unordered_set<int> interSet(nums1.begin(), nums1.end());
        vector<int> result;
        
        for(auto num : nums2){
            if(interSet.count(num)){
                result.push_back(num);
                interSet.erase(num);
            }
        }
        
        return result;
    }
};

相关文章推荐

349. Intersection of Two Arrays

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

LeetCode 349. Intersection of Two Arrays

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

leetcode解题之349 & 350. Intersection of Two Arrays Java版(求数组交集))

350. Intersection of Two Arrays II leetcode解题之349. Intersection of Two Arrays & 350. Intersection ...

leetcode349~Intersection of Two Arrays

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

349. Intersection of Two Arrays

Given two arrays, write a function to compute their intersection. Example: Given nums1 = [1, 2, ...
  • zshouyi
  • zshouyi
  • 2017年01月04日 04:07
  • 75

【Leetcode】349. Intersection of Two Arrays

方法一: 思路: 为避免重复,用Set存储后再存入结果数组。 遍历nums1,对nums2的每一个元素,若找到与nums1对应元素相等的元素且在Set中未出现,则加入Set。 public clas...

349. Intersection of Two Arrays I &&II (Java)

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

[LeetCode-349] Intersection of Two Arrays(java)

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

349. Intersection of Two Arrays [easy] (Python)

题目链接https://leetcode.com/problems/intersection-of-two-arrays/题目原文 Given two arrays, write a functi...

LeetCode349 Intersection of Two Arrays

原题Given two arrays, write a function to compute their intersection. Example: Given nums1 = [1, 2, ...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:349. Intersection of Two Arrays
举报原因:
原因补充:

(最多只允许输入30个字)