LeetCode刷题:349. Intersection of Two Arrays&350. Intersection of Two Arrays II

LeetCode刷题:349. Intersection of Two Arrays&350. Intersection of Two Arrays II

原题链接:

349. Intersection of Two Arrays https://leetcode.com/problems/intersection-of-two-arrays/

350. Intersection of Two Arrays II https://leetcode.com/problems/intersection-of-two-arrays-ii/


349. Intersection of Two Arrays

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

Example 1:

Input: nums1 = [1,2,2,1], nums2 = [2,2]
Output: [2]
Example 2:

Input: nums1 = [4,9,5], nums2 = [9,4,9,8,4]
Output: [9,4]
Note:

Each element in the result must be unique.
The result can be in any order.


算法设计

package com.bean.algorithm.basic;

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

public class InteractionOfTwoArray {

	public int[] intersection(int[] nums1, int[] nums2) {
		int len1 = nums1.length;
		int len2 = nums2.length;
		int i = 0;
		Set s1 = new HashSet<>();
		Set s2 = new HashSet<>();
		for (int j = 0; j < len1; j++) {
			s1.add(nums1[j]);
		}
		for (int k = 0; k < len2; k++) {
			if (s1.contains(nums2[k])) {
				s2.add(nums2[k]);
			}
		}
		int[] res = new int[s2.size()];
		for (int r : s2) {
			res[i] = r;
			i++;
		}
		return res;
	}

	public static void main(String[] args) {

		InteractionOfTwoArray inter = new InteractionOfTwoArray();
		// int[] nums1 =new int[] {1,2,2,1};
		// int[] nums2 =new int[] {2,2};

		int[] nums1 = new int[] { 4, 9, 5 };
		int[] nums2 = new int[] { 9, 4, 9, 8, 4 };

		int[] result = inter.intersection(nums1, nums2);
		for (int i = 0; i < result.length; i++) {
			System.out.print(result[i] + " ");
		}
		System.out.println();

	}

}

350. Intersection of Two Arrays II

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

Example 1:

Input: nums1 = [1,2,2,1], nums2 = [2,2]
Output: [2,2]
Example 2:

Input: nums1 = [4,9,5], nums2 = [9,4,9,8,4]
Output: [4,9]
Note:

Each element in the result should appear as many times as it shows in both arrays.
The result can be in any order.
Follow up:

What if the given array is already sorted? How would you optimize your algorithm?
What if nums1's size is small compared to nums2's size? Which algorithm is better?
What if elements of nums2 are stored on disk, and the memory is limited such that you cannot load all elements into the memory at once?


算法设计

package com.bean.algorithm.basic;

import java.util.ArrayList;

public class InteractionOfTwoArray2 {

	public int[] intersect(int[] nums1, int[] nums2) {
		ArrayList ar = new ArrayList();
		for (int i = 0; i < nums1.length; i++) {
			for (int j = 0; j < nums2.length; j++) {
				if (nums1[i] == nums2[j]) {
					ar.add(nums1[i]);
					nums2[j] = Integer.MIN_VALUE;
					nums1[i] = Integer.MAX_VALUE;
				}
			}
		}

		int[] rar = new int[ar.size()];
		for (int i = 0; i < ar.size(); i++) {
			rar[i] = ar.get(i);
		}

		return rar;
	}

	public static void main(String[] args) {

		InteractionOfTwoArray2 inter = new InteractionOfTwoArray2();
		// int[] nums1 =new int[] {1,2,2,1};
		// int[] nums2 =new int[] {2,2};

		int[] nums1 = new int[] { 4, 9, 5 };
		int[] nums2 = new int[] { 9, 4, 9, 8, 4 };

		int[] result = inter.intersect(nums1, nums2);
		for (int i = 0; i < result.length; i++) {
			System.out.print(result[i] + " ");
		}
		System.out.println();

	}

}

 

你可能感兴趣的:(算法分析与设计,LeetCode)