【leetcode】349. Intersection of Two Arrays

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.

我们可以使用两个指针来做,先给两个数组排序,然后用两个指针分别指向两个数组的开头,

然后比较两个数组的大小,把小的数字的指针向后移,如果两个指针指的数字相等,就加入到结果中。

但是结果中的数字不可以重复,可以用unique然后erase。(好像有些麻烦)也可以在结果中判断最后一个数字

是不是和当前的数字相等(因为已经sort过了)或者结果是不是empty,记住判断empty要在前面,否则直接

对一个空的vector求back会报错。

class Solution {
public:
    vector<int> intersection(vector<int>& nums1, vector<int>& nums2) {
        vector<int> ret;
        if(nums1.size()==0||nums2.size()==0)
            return ret;

        sort(nums1.begin(),nums1.end());
        sort(nums2.begin(),nums2.end());

        auto i=nums1.begin();
        auto j=nums2.begin();
        while(i!=nums1.end()&&j!=nums2.end()){
            if(*i<*j)
                i++;
            else if(*i>*j)
                j++;
            else {
                if(ret.empty()||ret.back()!=*i)
                    ret.push_back(*i);
                i++;
                j++;
            }
        }
        return ret;
    }
};
时间: 2024-07-28 20:10:37

【leetcode】349. Intersection of Two Arrays的相关文章

【LeetCode】 349. Intersection of Two Arrays 解题小结

题目: Given two arrays, write a function to compute their intersection. Example:Given nums1 = [1, 2, 2, 1], nums2 = [2, 2], return [2]. 第一次做的时候先将nums1存储在一个unordered_map里,然后对nums2的元素判断是否在unordered_map中,如果在,放入新的集合中,然后遍历集合,把结果输出,这样运算次数就是3N了:其实可以在第二次循环的时候判

【leetcode】350. Intersection of Two Arrays II

problem 350. Intersection of Two Arrays II 参考 1. Leetcode_350. Intersection of Two Arrays II; 完 原文地址:https://www.cnblogs.com/happyamyhope/p/10430296.html

【一天一道LeetCode】#349. Intersection of Two Arrays

一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 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

【LeetCode】350. Intersection of Two Arrays II 解题小结

题目: Given two arrays, write a function to compute their intersection. Example:Given nums1 = [1, 2, 2, 1], nums2 = [2, 2], return [2, 2]. 相比较I,这个题目允许答案出现重复的.想法就是做一个map统计nums1元素出现次数,然后nums2每次遍历都看看元素出现次数. class Solution { public: vector<int> intersect(

【题解】【数组】【Leetcode】Median of Two Sorted Arrays

Median of Two Sorted Arrays There are two sorted arrays A and B of size m and n respectively. Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)). ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22

[LeetCode] NO. 349 Intersection of Two Arrays

[题目] Given two arrays, write a function to compute their intersection. Example:Given nums1 = [1, 2, 2, 1], nums2 = [2, 2], return [2]. [题目解析] 这是一个很简单的求交集的问题,可以用Set来解决.如下. public static int[] intersection(int[] nums1, int[] nums2){ Set<Integer> set =

【LeetCode】Median of Two Sorted Arrays

题目描述 问题分析 代码 总结 个人声明 题目描述 There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)). 问题分析 首先假设数组A和B的元素个数都大于k/2,我们比较A[k/2-1]和B[k/2-1]两个元素,

【leetcode】Median of Two Sorted Arrays(hard)★!!

There are two sorted arrays A and B of size m and n respectively. Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)). 思路: 难,知道用分治算法,却不知道怎么用.只好看答案. 基本的思路是如果中位数是第K个数,A[i]如果是中位数,那么A[i]已经大于了i个数,还应大于K - i - 1个

【LeetCode】Median of Two Sorted Arrays (2 solutions)

Median of Two Sorted Arrays There are two sorted arrays A and B of size m and n respectively. Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)). 解法一:保底做法,O(m+n)复杂度 按照归并排序的思路,数到median,然后计算返回. 需要注意: 如果是m+n