[LeetCode] NO. 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].

[题目解析] 这是对于求两个数组交集的延伸,之前求数组交集重复元素不包含在交集内,用set来求。那么这个问题,就可以用hashmap来解决。如下。

    public int[] intersect(int[] nums1, int[] nums2) {
        HashMap<Integer,Integer> map = new HashMap<Integer,Integer>();
        List<Integer> lst = new ArrayList<Integer>();
        for(int num : nums1){
            if(map.containsKey(num)){
                map.put(num, map.get(num)+1);
            }else{
                map.put(num,1);
            }
        }
        for(int num : nums2){
            if(map.containsKey(num)){
                lst.add(num);
                int count = map.get(num);
                count--;
                if(count < 1){
                    map.remove(num);
                }else{
                    map.put(num, count);
                }
            }
        }
        int[] result = new int[lst.size()];
        for(int i = 0; i < lst.size(); i++){
            result[i] = lst.get(i);
        }
        return result;
    }

进一步思考,我们还可以想到另外一种方法,先对两个数组进行排序,两个有序数组求公共部分,遍历的时候参考归并排序的思想,如下所示。

public int[] intersect(int[] nums1, int[] nums2) {
    Arrays.sort(nums1);
    Arrays.sort(nums2);
    List<Integer> temp = new ArrayList<Integer>();
    for (int i = 0, j = 0; i < nums1.length && j < nums2.length;){
        if (nums1[i] < nums2[j]) i++;
        else if (nums1[i] > nums2[j]) j++;
        else {
            temp.add(nums1[i]);
            i++; j++;
        }
    }
    int[] res = new int[temp.size()];
    for (int i = 0; i < temp.size(); i++)
        res[i] = temp.get(i);
    return res;
}
时间: 2024-08-02 02:49:09

[LeetCode] NO. 350 Intersection of Two Arrays II的相关文章

【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】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记-350. Intersection of Two Arrays II

今天题目如下: 比较简单,代码如下: /** * @param {number[]} nums1 * @param {number[]} nums2 * @return {number[]} */ var intersect = function(nums1, nums2) { let myMap = new Map(); let myArr = new Array(); nums1.forEach(function(item){ if(myMap.has(item)){ myMap.set(i

[LeetCode] 349 Intersection of Two Arrays &amp; 350 Intersection of Two Arrays II

这两道题都是求两个数组之间的重复元素,因此把它们放在一起. 原题地址: 349 Intersection of Two Arrays :https://leetcode.com/problems/intersection-of-two-arrays/description/ 350 Intersection of Two Arrays II:https://leetcode.com/problems/intersection-of-two-arrays-ii/description/ 题目&解法

leetcode349 350 Intersection of Two Arrays &amp; II

1 """ 2 Intersection of Two Arrays 3 Given two arrays, write a function to compute their intersection. 4 Example 1: 5 Input: nums1 = [1,2,2,1], nums2 = [2,2] 6 Output: [2] 7 Example 2: 8 Input: nums1 = [4,9,5], nums2 = [9,4,9,8,4] 9 Output:

350. Intersection of Two Arrays II(LeetCode)

Given two arrays, write a function to compute their intersection. Example:Given nums1 = [1, 2, 2, 1], nums2 = [2, 2], return [2, 2]. Note: Each element in the result should appear as many times as it shows in both arrays. The result can be in any ord

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]. Note: Each element in the result should appear as many times as it shows in both arrays. The result can be in any ord

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]. Note: Each element in the result should appear as many times as it shows in both arrays. The result can be in

Leetcode 350. Intersection of Two Arrays II JAVA语言

Given two arrays, write a function to compute their intersection. Example: Given nums1 = [1, 2, 2, 1], nums2 = [2, 2], return [2, 2]. Note: Each element in the result should appear as many times as it shows in both arrays. The result can be in any or