# LeetCode 350. 两个数组的交集 II Intersection of Two Arrays II

### 4-2 map的使用 Intersection of Two Arrays II

##### 题目: LeetCode 350. 两个数组的交集 II

import java.util.TreeMap;
import java.util.ArrayList;

// 350. Intersection of Two Arrays II
// https://leetcode.com/problems/intersection-of-two-arrays-ii/description/
// 时间复杂度: O(nlogn)
// 空间复杂度: O(n)
public class Solution {

public int[] intersect(int[] nums1, int[] nums2) {

TreeMap<Integer, Integer> record = new TreeMap<Integer, Integer>();
for(int num: nums1)
if(!record.containsKey(num))
record.put(num, 1);
else
record.put(num, record.get(num) + 1);

ArrayList<Integer> result = new ArrayList<Integer>();
for(int num: nums2)
if(record.containsKey(num) && record.get(num) > 0){
record.put(num, record.get(num) - 1);
}

int[] ret = new int[result.size()];
int index = 0;
for(Integer num: result)
ret[index++] = num;

return ret;
}

private static void printArr(int[] arr){
for(int e: arr)
System.out.print(e + " ");
System.out.println();
}

public static void main(String[] args) {

int[] nums1 = {1, 2, 2, 1};
int[] nums2 = {2, 2};
int[] res = (new Solution()).intersect(nums1, nums2);
printArr(res);
}
}

/// 让我们来测试使用Java中的TreeMap:)

import java.util.TreeMap;

public class Main {

public static void main(String[] args) {

TreeMap<Integer, Integer> myMap = new TreeMap<Integer, Integer>();
if(myMap.containsKey(42))
System.out.println("Element 42 is in the map");
else
System.out.println("Can not find element 42");

System.out.println(myMap.get(42)); // 输出 null

// Java不存在C++中默认的访问key即添加默认(key, value)的行为
// 以下代码仍然无法找到42
if(myMap.containsKey(42))
System.out.println("Element 42 is in the map");
else
System.out.println("Can not find element 42");

myMap.put(42, 0);
myMap.put(42, myMap.get(42) + 1);
System.out.println(myMap.get(42)); // 输出 1
if(myMap.containsKey(42))
System.out.println("Element 42 is in the map");
else
System.out.println("Can not find element 42");

myMap.put(42, myMap.get(42) - 1);
System.out.println(myMap.get(42)); // 输出 0

// 注意: key对应的值为0, 不代表key不存在
if(myMap.containsKey(42))
System.out.println("Element 42 is in the map");
else
System.out.println("Can not find element 42");

// 注意： 也不可以为key对应的值设置null来删除一个key
myMap.put(42, null);
if(myMap.containsKey(42))
System.out.println("Element 42 is in the map");
else
System.out.println("Can not find element 42");

// 使用remove删除一个key
myMap.remove(42);
if(myMap.containsKey(42))
System.out.println("Element 42 is in the map");
else
System.out.println("Can not find element 42");
}
}

import java.util.HashMap;
import java.util.ArrayList;

// 350. Intersection of Two Arrays II
// https://leetcode.com/problems/intersection-of-two-arrays-ii/description/
// 时间复杂度: O(len(nums1)+len(nums2))
// 空间复杂度: O(len(nums1))
public class Solution350 {

public int[] intersect(int[] nums1, int[] nums2) {

HashMap<Integer, Integer> record = new HashMap<Integer, Integer>();
for(int num: nums1)
if(!record.containsKey(num))
record.put(num, 1);
else
record.put(num, record.get(num) + 1);

ArrayList<Integer> result = new ArrayList<Integer>();
for(int num: nums2)
if(record.containsKey(num) && record.get(num) > 0){
record.put(num, record.get(num) - 1);
}

int[] ret = new int[result.size()];
int index = 0;
for(Integer num: result)
ret[index++] = num;

return ret;
}

private static void printArr(int[] arr){
for(int e: arr)
System.out.print(e + " ");
System.out.println();
}

public static void main(String[] args) {

int[] nums1 = {1, 2, 2, 1};
int[] nums2 = {2, 2};
int[] res = (new Solution350()).intersect(nums1, nums2);
printArr(res);
}
}


05-25 1207

05-24 4324

01-18 465

06-21 269

07-13 185

01-14 23

04-30 141

03-16 85

04-26 481

06-26 2019

#### Intersection of Two Arrays II两个数组交集（重要！）

©️2020 CSDN 皮肤主题: 大白 设计师: CSDN官方博客

1.余额是钱包充值的虚拟货币，按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载，可以购买VIP、C币套餐、付费专栏及课程。