# LeetCode之Intersection of Two Arrays

### 1、题目

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

Example:

Given nums1 = [1, 2, 2, 1], nums2 = [2, 2], return [2].

Note:

Each element in the result must be unique.

The result can be in any order.

Subscribe to see which companies asked this question.

### 2、代码实现

public class Solution {

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

if (nums1 == null || nums2 == null) {

return null;

}

Map<Integer, Integer> map = new HashMap<Integer, Integer>();

List<Integer> list = new ArrayList<Integer>();

for (int  i = 0; i < nums1.length; ++i) {

map.put(nums1[i], 1);

}

for (int i = 0; i < nums2.length; ++i) {

if (map.get(nums2[i]) != null && map.get(nums2[i]) != 2) {

map.put(nums2[i], 2);

}

}

int a[] = new int[list.size()];

for(int i = 0, j = list.size(); i < j; i++){

a[i] = list.get(i);

}

return a;

}

}

### 3、总结

if (map.get(nums2[i]) != null) {

}

|
11月前
|

LeetCode刷题---Two Sum（一）
LeetCode刷题---Two Sum（一）
|
9月前
Leetcode 4. Median of Two Sorted Arrays

21 0
|
11月前
|

|

LeetCode - #2 Add Two Numbers

64 0
|

LeetCode - #1 Two Sum

48 0
LeetCode 350. Intersection of Two Arrays II

60 0
|
Python
LeetCode 349. Intersection of Two Arrays

59 0
LeetCode 160. Intersection of Two Linked Lists

54 0
LeetCode 167 Two Sum II - Input array is sorted（输入已排序数组，求其中两个数的和等于给定的数）

72 0
|
Python
LeetCode 21. 合并两个有序链表 Merge Two Sorted Lists
LeetCode 21. 合并两个有序链表 Merge Two Sorted Lists
81 0