Skip to content

Commit 0aa4f62

Browse files
committed
commit solution 349
1 parent 25ba5a3 commit 0aa4f62

File tree

6 files changed

+126
-2
lines changed

6 files changed

+126
-2
lines changed

Diff for: index-tags.md

+4
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@
7979
| [219](https://leetcode-cn.com/problems/contains-duplicate-ii) | [存在重复元素 ii](/solution/200-299/0219.contains-duplicate-ii/) | `数组`,`哈希表` | <font color=green>简单</font> ||
8080
| [290](https://leetcode-cn.com/problems/word-pattern) | [单词规律](/solution/200-299/0290.word-pattern/) | `哈希表` | <font color=green>简单</font> ||
8181
| [299](https://leetcode-cn.com/problems/bulls-and-cows) | [猜数字游戏](/solution/200-299/0299.bulls-and-cows/) | `哈希表` | <font color=green>简单</font> ||
82+
| [349](https://leetcode-cn.com/problems/intersection-of-two-arrays) | [两个数组的交集](/solution/300-399/0349.intersection-of-two-arrays/) | `排序`,`哈希表`,`双指针`,`二分查找` | <font color=green>简单</font> ||
8283

8384
#### ****
8485

@@ -108,6 +109,7 @@
108109
| [283](https://leetcode-cn.com/problems/move-zeroes) | [移动零](/solution/200-299/0283.move-zeroes/) | `数组`,`双指针` | <font color=green>简单</font> ||
109110
| [344](https://leetcode-cn.com/problems/reverse-string) | [反转字符串](/solution/300-399/0344.reverse-string/) | `双指针`,`字符串` | <font color=green>简单</font> ||
110111
| [345](https://leetcode-cn.com/problems/reverse-vowels-of-a-string) | [反转字符串中的元音字母](/solution/300-399/0345.reverse-vowels-of-a-string/) | `双指针`,`字符串` | <font color=green>简单</font> ||
112+
| [349](https://leetcode-cn.com/problems/intersection-of-two-arrays) | [两个数组的交集](/solution/300-399/0349.intersection-of-two-arrays/) | `排序`,`哈希表`,`双指针`,`二分查找` | <font color=green>简单</font> ||
111113

112114
#### **链表**
113115

@@ -130,13 +132,15 @@
130132
| [167](https://leetcode-cn.com/problems/two-sum-ii-input-array-is-sorted) | [两数之和 ii - 输入有序数组](/solution/100-199/0167.two-sum-ii---input-array-is-sorted/) | `数组`,`双指针`,`二分查找` | <font color=green>简单</font> ||
131133
| [209](https://leetcode-cn.com/problems/minimum-size-subarray-sum) | [长度最小的子数组](/solution/200-299/0209.minimum-size-subarray-sum/) | `数组`,`双指针`,`二分查找` | <font color=blue>中等</font> ||
132134
| [278](https://leetcode-cn.com/problems/first-bad-version) | [第一个错误的版本](/solution/200-299/0278.first-bad-version/) | `二分查找` | <font color=green>简单</font> ||
135+
| [349](https://leetcode-cn.com/problems/intersection-of-two-arrays) | [两个数组的交集](/solution/300-399/0349.intersection-of-two-arrays/) | `排序`,`哈希表`,`双指针`,`二分查找` | <font color=green>简单</font> ||
133136

134137
#### **排序**
135138

136139
| 题号 | 题解 | 标签 | 难度 | 是否解题 |
137140
| --- | --- | --- | --- | --- |
138141
| [56](https://leetcode-cn.com/problems/merge-intervals) | [合并区间](/solution/1-99/0056.merge-intervals/) | `排序`,`数组` | <font color=blue>中等</font> ||
139142
| [75](https://leetcode-cn.com/problems/sort-colors) | [颜色分类](/solution/1-99/0075.sort-colors/) | `排序`,`数组`,`双指针` | <font color=blue>中等</font> ||
143+
| [349](https://leetcode-cn.com/problems/intersection-of-two-arrays) | [两个数组的交集](/solution/300-399/0349.intersection-of-two-arrays/) | `排序`,`哈希表`,`双指针`,`二分查找` | <font color=green>简单</font> ||
140144

141145
#### **队列**
142146

Diff for: index-type.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@
103103
| [344](https://leetcode-cn.com/problems/reverse-string) | [反转字符串](/solution/300-399/0344.reverse-string/) | `双指针`,`字符串` | <font color=green>简单</font> ||
104104
| [345](https://leetcode-cn.com/problems/reverse-vowels-of-a-string) | [反转字符串中的元音字母](/solution/300-399/0345.reverse-vowels-of-a-string/) | `双指针`,`字符串` | <font color=green>简单</font> ||
105105
| [346](https://leetcode-cn.com/problems/moving-average-from-data-stream) | [数据流中的移动平均值](/solution/300-399/0346.moving-average-from-data-stream/) | `设计`,`队列` | <font color=green>简单</font> |
106-
| [349](https://leetcode-cn.com/problems/intersection-of-two-arrays) | [两个数组的交集](/solution/300-399/0349.intersection-of-two-arrays/) | `排序`,`哈希表`,`双指针`,`二分查找` | <font color=green>简单</font> |
106+
| [349](https://leetcode-cn.com/problems/intersection-of-two-arrays) | [两个数组的交集](/solution/300-399/0349.intersection-of-two-arrays/) | `排序`,`哈希表`,`双指针`,`二分查找` | <font color=green>简单</font> ||
107107
| [350](https://leetcode-cn.com/problems/intersection-of-two-arrays-ii) | [两个数组的交集 ii](/solution/300-399/0350.intersection-of-two-arrays-ii/) | `排序`,`哈希表`,`双指针`,`二分查找` | <font color=green>简单</font> |
108108
| [359](https://leetcode-cn.com/problems/logger-rate-limiter) | [日志速率限制器](/solution/300-399/0359.logger-rate-limiter/) | `设计`,`哈希表` | <font color=green>简单</font> |
109109
| [367](https://leetcode-cn.com/problems/valid-perfect-square) | [有效的完全平方数](/solution/300-399/0367.valid-perfect-square/) | `数学`,`二分查找` | <font color=green>简单</font> |
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
# [349. 两个数组的交集](https://leetcode-cn.com/problems/intersection-of-two-arrays/description/)
2+
3+
### 题目描述
4+
5+
<p>给定两个数组,编写一个函数来计算它们的交集。</p>
6+
7+
<p>&nbsp;</p>
8+
9+
<p><strong>示例 1:</strong></p>
10+
11+
<pre><strong>输入:</strong>nums1 = [1,2,2,1], nums2 = [2,2]
12+
<strong>输出:</strong>[2]
13+
</pre>
14+
15+
<p><strong>示例 2:</strong></p>
16+
17+
<pre><strong>输入:</strong>nums1 = [4,9,5], nums2 = [9,4,9,8,4]
18+
<strong>输出:</strong>[9,4]</pre>
19+
20+
<p>&nbsp;</p>
21+
22+
<p><strong>说明:</strong></p>
23+
24+
<ul>
25+
<li>输出结果中的每个元素一定是唯一的。</li>
26+
<li>我们可以不考虑输出结果的顺序。</li>
27+
</ul>
28+
29+
### 解题思路
30+
31+
1. hash table
32+
33+
### 具体解法
34+
35+
36+
#### **Golang**
37+
```go
38+
func intersection(nums1 []int, nums2 []int) []int {
39+
if len(nums1) == 0 || len(nums2) == 0 {
40+
return []int{}
41+
}
42+
num1Map := make(map[int]bool)
43+
44+
for i := range nums1 {
45+
num1Map[nums1[i]] = true
46+
}
47+
num2Map := make(map[int]bool)
48+
var res []int
49+
for i := range nums2 {
50+
if _, ok1 := num1Map[nums2[i]]; ok1 && !num2Map[nums2[i]] {
51+
num2Map[nums2[i]] = true
52+
res = append(res, nums2[i])
53+
}
54+
}
55+
return res
56+
}
57+
```
58+
59+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package leetcode
2+
3+
/*
4+
* @lc app=leetcode.cn id=349 lang=golang
5+
*
6+
* [349] 两个数组的交集
7+
*/
8+
9+
// @lc code=start
10+
func intersection(nums1 []int, nums2 []int) []int {
11+
if len(nums1) == 0 || len(nums2) == 0 {
12+
return []int{}
13+
}
14+
num1Map := make(map[int]bool)
15+
16+
for i := range nums1 {
17+
num1Map[nums1[i]] = true
18+
}
19+
num2Map := make(map[int]bool)
20+
var res []int
21+
for i := range nums2 {
22+
if _, ok1 := num1Map[nums2[i]]; ok1 && !num2Map[nums2[i]] {
23+
num2Map[nums2[i]] = true
24+
res = append(res, nums2[i])
25+
}
26+
}
27+
return res
28+
}
29+
30+
// @lc code=end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package leetcode
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
)
7+
8+
func TestIntersection(t *testing.T) {
9+
var nums1 []int
10+
var nums2 []int
11+
var ret []int
12+
13+
nums1 = []int{1, 2, 2, 1}
14+
nums2 = []int{2, 2}
15+
ret = []int{2}
16+
for i, v := range intersection(nums1, nums2) {
17+
if v != ret[i] {
18+
t.Fatalf("case fails %v\n", ret)
19+
}
20+
}
21+
22+
nums1 = []int{}
23+
nums2 = []int{2, 2}
24+
ret = []int{}
25+
fmt.Println(intersection(nums1, nums2))
26+
for i, v := range intersection(nums1, nums2) {
27+
if v != ret[i] {
28+
t.Fatalf("case fails %v\n", ret)
29+
}
30+
}
31+
}

Diff for: solution/300-399/_sidebar.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@
5555
- [346. 数据流中的移动平均值](solution/300-399/0346.moving-average-from-data-stream/)
5656
- [347. 前 k 个高频元素](solution/300-399/0347.top-k-frequent-elements/)
5757
- [348. 判定井字棋胜负](solution/300-399/0348.design-tic-tac-toe/)
58-
- [349. 两个数组的交集](solution/300-399/0349.intersection-of-two-arrays/)
58+
- [349. 两个数组的交集](solution/300-399/0349.intersection-of-two-arrays/)
5959
- [350. 两个数组的交集 ii](solution/300-399/0350.intersection-of-two-arrays-ii/)
6060
- [351. 安卓系统手势解锁](solution/300-399/0351.android-unlock-patterns/)
6161
- [352. 将数据流变为多个不相交区间](solution/300-399/0352.data-stream-as-disjoint-intervals/)

0 commit comments

Comments
 (0)