【算法】查找類問題--兩個數組的交集

查找類問題主要有兩類:

  1. 查找有無
    • 元素‘a’ 是否存在?這種情況經常使用的數據結構是 set
  2. 查找對應關係(簡直對應)
    • 元素‘a’出現了幾次?這種情況下一般使用 map

示例1:

package solution.search;

import leetcode.array.important.Intersect;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

/**
 * @description 349. 兩個數組的交集
 * 給定兩個數組,編寫一個函數來計算它們的交集。
 * <p>
 * 示例 1:
 * 輸入: nums1 = [1,2,2,1], nums2 = [2,2]
 * 輸出: [2]
 * <p>
 * 示例 2:
 * 輸入: nums1 = [4,9,5], nums2 = [9,4,9,8,4]
 * 輸出: [9,4]
 * <p>
 * 說明:
 * 輸出結果中的每個元素一定是唯一的。
 * 我們可以不考慮輸出結果的順序。
 * <p>
 * 來源:力扣(LeetCode)
 * 鏈接:https://leetcode-cn.com/problems/intersection-of-two-arrays
 * 著作權歸領釦網絡所有。商業轉載請聯繫官方授權,非商業轉載請註明出處。
 * @className Intersection349
 */
public class Intersection349 {

    public static void main(String[] args) {
        int[] nums1 = {4, 9, 5}, nums2 = {9, 4, 9, 8, 4};
        System.out.println(Arrays.toString(intersection(nums1, nums2)));
    }

    /**
     * 典型的查找類問題
     * 使用set
     *
     * @param nums1 數組1
     * @param nums2 數組2
     * @return int[]
     */
    private static int[] intersection(int[] nums1, int[] nums2) {
        Set<Integer> set1 = new HashSet<>();
        for (int i : nums1) {
            set1.add(i);
        }
        Set<Integer> set2 = new HashSet<>();
        for (int num : nums2) {
            if (set1.contains(num)) {
                set2.add(num);
            }
        }
        int[] nums3 = new int[set2.size()];
        if (set2.size() > 0) {
            int index = 0;
            for (Integer integer : set2) {
                nums3[index++] = integer;
            }
        }
        return nums3;
    }
}

示例2:

package solution.search;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @description 350. 兩個數組的交集 II
 * 給定兩個數組,編寫一個函數來計算它們的交集。
 * <p>
 * 示例 1:
 * 輸入: nums1 = [1,2,2,1], nums2 = [2,2]
 * 輸出: [2,2]
 * <p>
 * 示例 2:
 * 輸入: nums1 = [4,9,5], nums2 = [9,4,9,8,4]
 * 輸出: [4,9]
 * <p>
 * 說明:
 * 輸出結果中每個元素出現的次數,應與元素在兩個數組中出現的次數一致。
 * 我們可以不考慮輸出結果的順序。
 * <p>
 * 進階:
 * 如果給定的數組已經排好序呢?你將如何優化你的算法?
 * 如果 nums1 的大小比 nums2 小很多,哪種方法更優?
 * 如果 nums2 的元素存儲在磁盤上,磁盤內存是有限的,並且你不能一次加載所有的元素到內存中,你該怎麼辦?
 * <p>
 * 來源:力扣(LeetCode)
 * 鏈接:https://leetcode-cn.com/problems/intersection-of-two-arrays-ii
 * 著作權歸領釦網絡所有。商業轉載請聯繫官方授權,非商業轉載請註明出處。
 * @className Intersect350
 */
public class Intersect350 {

    public static void main(String[] args) {
        int[] nums1 = {4, 9, 5}, nums2 = {9, 4, 9, 8, 4};
        System.out.println(Arrays.toString(intersect(nums1, nums2)));
    }

    private static int[] intersect(int[] nums1, int[] nums2) {
        List<Integer> list1 = new ArrayList<>();
        for (int i : nums1) {
            list1.add(i);
        }
        List<Integer> list = new ArrayList<>();
        for (int num : nums2) {
            if (list1.contains(num)) {
                list.add(num);
                list1.remove(Integer.valueOf(num));
            }
        }
        int[] nums3 = new int[list.size()];
        if (list.size() > 0) {
            int index = 0;
            for (Integer integer : list) {
                nums3[index++] = integer;
            }
        }
        return nums3;
    }
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章