⬆︎
×

[LC] 0350 Intersection of Two Arrays II

Java

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

/**
 * <a href="https://leetcode.cn/problems/intersection-of-two-arrays-ii/">Intersection of Two Arrays II</a>
 * 数组;哈希表;双指针;二分查找;排序
 */
class Solution {
    public int[] intersect(int[] nums1, int[] nums2) {
        if (nums1.length > nums2.length) {
            return intersect(nums2, nums1);
        }

        Map<Integer, Integer> map = new HashMap<>();
        for (int num : nums1) {
            int cnt = map.getOrDefault(num, 0) + 1;
            map.put(num, cnt);
        }

        int[] res = new int[nums1.length];
        int len = 0;
        for (int num : nums2) {
            int cnt = map.getOrDefault(num, 0);
            if (cnt > 0) {
                res[len++] = num;
                cnt--;
                if (cnt > 0) {
                    map.put(num, cnt);
                } else {
                    map.remove(num);
                }
            }
        }
        return Arrays.copyOfRange(res, 0, len);
    }
}

Go

package intersection_of_two_arrays_ii

func intersect(nums1 []int, nums2 []int) []int {
    if len(nums1) > len(nums2) {
        return intersect(nums2, nums1)
    }

    m := make(map[int]int)
    for _, num := range nums1 {
        m[num]++
    }

    res := make([]int, 0)
    for _, num := range nums2 {
        cnt := m[num]
        if cnt > 0 {
            res = append(res, num)
            cnt--
            if cnt > 0 {
                m[num] = cnt
            } else {
                delete(m, num)
            }
        }
    }
    return res
}

JavaScript

/**
 * @param {number[]} nums1
 * @param {number[]} nums2
 * @return {number[]}
 */
var intersect = function (nums1, nums2) {
    if (nums1.length > nums2.length) {
        return intersect(nums2, nums1);
    }

    let map = new Map();
    for (const num of nums1) {
        const cnt = map.get(num) || 0;
        map.set(num, cnt + 1);
    }

    let res = [];
    for (const num of nums2) {
        let cnt = map.get(num) || 0;
        if (cnt > 0) {
            res.push(num);
            cnt--;
            if (cnt > 0) {
                map.set(num, cnt);
            } else {
                map.delete(num);
            }
        }
    }
    return res;
};

PHP

class Solution {

    /**
     * @param Integer[] $nums1
     * @param Integer[] $nums2
     * @return Integer[]
     */
    function intersect(array $nums1, array $nums2): array {
        if (count($nums1) > count($nums2)) {
            return $this->intersect($nums2, $nums1);
        }

        $map = [];
        foreach ($nums1 as $num) {
            $map[$num] = ($map[$num] ?? 0) + 1;
        }

        $res = [];
        foreach ($nums2 as $num) {
            $cnt = $map[$num] ?? 0;
            if ($cnt > 0) {
                $res[] = $num;
                $cnt--;
                if ($cnt > 0) {
                    $map[$num] = $cnt;
                } else {
                    unset($map[$num]);
                }
            }
        }
        return $res;
    }
}

发表评论