Skip to content

Commit 3e85fc5

Browse files
committed
add 350
1 parent 1898888 commit 3e85fc5

File tree

3 files changed

+105
-0
lines changed

3 files changed

+105
-0
lines changed

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
| 189 | [Rotate Array][189] |
2222
| 217 | [Contains Duplicate][217] |
2323
| 136 | [Single Number][136] |
24+
| 350 | [Intersection of Two Arrays II][350] |
2425

2526
[leetcode]: https://leetcode.com/problemset/all/
2627
[blankj]: https://github.com/Blankj/awesome-java-leetcode
@@ -32,3 +33,4 @@
3233
[189]: https://github.com/andavid/leetcode-java/blob/master/note/189/README.md
3334
[217]: https://github.com/andavid/leetcode-java/blob/master/note/217/README.md
3435
[136]: https://github.com/andavid/leetcode-java/blob/master/note/136/README.md
36+
[350]: https://github.com/andavid/leetcode-java/blob/master/note/350/README.md

note/350/README.md

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
# [Intersection of Two Arrays II][title]
2+
3+
## Description
4+
5+
Given two arrays, write a function to compute their intersection.
6+
7+
**Example:**
8+
Given nums1 = `[1, 2, 2, 1]`, nums2 = `[2, 2]`, return `[2, 2]`.
9+
10+
**Note:**
11+
* Each element in the result should appear as many times as it shows in both arrays.
12+
* The result can be in any order.
13+
14+
**Follow up:**
15+
* What if the given array is already sorted? How would you optimize your algorithm?
16+
* What if nums1's size is small compared to nums2's size? Which algorithm is better?
17+
* 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?
18+
19+
## 思路
20+
使用 HashMap 遍历第一个数组,并记录数组元素出现的次数。
21+
然后遍历第二个数组,如果数组元素在 HashMap 中,则把该元素添加到 List,同时 HashMap 该元素出现次数减一。
22+
最后把 List 转成 int 数组即可。
23+
时间复杂度 O(n),空间复杂度 O(n)。
24+
25+
## [完整代码][src]
26+
27+
```java
28+
class Solution {
29+
public int[] intersect(int[] nums1, int[] nums2) {
30+
Map<Integer, Integer> map = new HashMap<Integer, Integer>();
31+
List<Integer> list = new ArrayList<Integer>();
32+
33+
for (int num : nums1) {
34+
if (map.containsKey(num)) {
35+
map.put(num, map.get(num) + 1);
36+
} else {
37+
map.put(num, 1);
38+
}
39+
}
40+
41+
for (int num : nums2) {
42+
if (map.containsKey(num)) {
43+
if (map.get(num) > 0) {
44+
list.add(num);
45+
map.put(num, map.get(num) - 1);
46+
}
47+
}
48+
}
49+
50+
int size = list.size();
51+
int result[] = new int[size];
52+
for (int i = 0; i < size; i++) {
53+
result[i] = list.get(i);
54+
}
55+
56+
return result;
57+
}
58+
}
59+
```
60+
61+
[title]: https://leetcode.com/problems/intersection-of-two-arrays-ii
62+
[src]: https://github.com/andavid/leetcode-java/blob/master/src/com/andavid/leetcode/_350/Solution.java
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import java.util.*;
2+
3+
class Solution {
4+
public int[] intersect(int[] nums1, int[] nums2) {
5+
Map<Integer, Integer> map = new HashMap<Integer, Integer>();
6+
List<Integer> list = new ArrayList<Integer>();
7+
8+
for (int num : nums1) {
9+
if (map.containsKey(num)) {
10+
map.put(num, map.get(num) + 1);
11+
} else {
12+
map.put(num, 1);
13+
}
14+
}
15+
16+
for (int num : nums2) {
17+
if (map.containsKey(num)) {
18+
if (map.get(num) > 0) {
19+
list.add(num);
20+
map.put(num, map.get(num) - 1);
21+
}
22+
}
23+
}
24+
25+
int size = list.size();
26+
int result[] = new int[size];
27+
for (int i = 0; i < size; i++) {
28+
result[i] = list.get(i);
29+
}
30+
31+
return result;
32+
}
33+
34+
public static void main(String[] args) {
35+
Solution solution = new Solution();
36+
int[] nums = solution.intersect(new int[]{1, 2, 2, 1}, new int[]{2, 2});
37+
for (int i = 0; i < nums.length; i++) {
38+
System.out.print(i == 0 ? nums[i] : "," + nums[i]);
39+
}
40+
}
41+
}

0 commit comments

Comments
 (0)