Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit feef27e

Browse files
committed
eat: add leetcode question #349
1 parent 5af517b commit feef27e

File tree

8 files changed

+184
-3
lines changed

8 files changed

+184
-3
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.hi.dhl.algorithms.leetcode._349.java;
2+
3+
import java.util.HashSet;
4+
import java.util.Set;
5+
6+
/**
7+
* <pre>
8+
* author: dhl
9+
* date : 2020年11月13日
10+
* desc :
11+
* </pre>
12+
*/
13+
14+
class Solution {
15+
public int[] intersection(int[] nums1, int[] nums2) {
16+
17+
Set<Integer> set1 = new HashSet<>();
18+
for (int value : nums1) {
19+
set1.add(value);
20+
}
21+
22+
Set<Integer> set2 = new HashSet<Integer>();
23+
for (int value : nums2) {
24+
if (set1.contains(value)) {
25+
set2.add(value);
26+
}
27+
}
28+
29+
int[] result = new int[set2.size()];
30+
int index = 0;
31+
for (int value : set2) {
32+
result[index++] = value;
33+
}
34+
return result;
35+
}
36+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.hi.dhl.algorithms.leetcode._349.kotlin
2+
3+
/**
4+
* <pre>
5+
* author: dhl
6+
* date : 2020年11月13日
7+
* desc :
8+
* </pre>
9+
*/
10+
11+
class Solution {
12+
fun intersection(nums1: IntArray, nums2: IntArray): IntArray {
13+
val set1 = HashSet<Int>();
14+
for (value in nums1) {
15+
set1.add(value);
16+
}
17+
18+
val set2 = HashSet<Int>();
19+
for (value in nums2) {
20+
if (set1.contains(value)) {
21+
set2.add(value);
22+
}
23+
}
24+
25+
val result = IntArray(set2.size);
26+
var index = 0;
27+
for (value in set2) {
28+
result[index++] = value;
29+
}
30+
31+
return result;
32+
}
33+
}

‎README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
剑指 offer 及大厂面试题解:<a href ="https://offer.hi-dhl.com">在线阅读</a>,LeetCode 系列题解:<a href ="https://leetcode.hi-dhl.com">在线阅读</a>
77
</p>
88

9-
<p align="center"> 做题进度:AC 132 题,每道题目都会用 Java 和 kotlin 去实现</p>
9+
<p align="center"> 做题进度:AC 133 题,每道题目都会用 Java 和 kotlin 去实现</p>
1010

1111
<p align="center"> 每题都有解题思路、时间复杂度、空间复杂度、源代码</p>
1212

‎leetcode/_sidebar.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,4 +55,8 @@
5555

5656
* [回文数](/math/01-palindrome-number.md)
5757

58+
* 哈希表
59+
60+
* [两个数组的交集](/hash/01-intersection-of-two-arrays.md)
61+
5862

‎leetcode/common/index_header.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
<p align="center"> 仓库 <b>每周</b> 持续更新,如果对你有帮助,请在右上角 star 一下</p>
44

5-
<p align="center"> LeetCode 的题解进度,目前已经 AC 了 132 题</p>
5+
<p align="center"> LeetCode 的题解进度,目前已经 AC 了 133 题</p>
66

77
<p align="center"> 每题都有解题思路、时间复杂度、空间复杂度、源代码</p>
88

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
题目来源于 LeetCode 上第 349 号问题:两个数组的交集。题目难度为 Easy。
2+
3+
* [英文地址:https://leetcode.com/problems/intersection-of-two-arrays](https://leetcode.com/problems/intersection-of-two-arrays)
4+
5+
* [中文地址:https://leetcode-cn.com/problems/intersection-of-two-arrays](https://leetcode-cn.com/problems/intersection-of-two-arrays)
6+
7+
## 题目描述
8+
9+
给定两个数组,编写一个函数来计算它们的交集。
10+
11+
**示例1:**
12+
13+
```
14+
输入:nums1 = [1,2,2,1], nums2 = [2,2]
15+
输出:[2]
16+
```
17+
18+
**示例2:**
19+
20+
```
21+
输入:nums1 = [4,9,5], nums2 = [9,4,9,8,4]
22+
输出:[9,4]
23+
```
24+
25+
说明:
26+
27+
* 输出结果中的每个元素一定是唯一的。
28+
* 我们可以不考虑输出结果的顺序。
29+
30+
## 思路一:Hash 算法
31+
32+
因为两个数组中都有可能出现重复的元素,所以要保证元素都是唯一的,需要用到哈希集合
33+
34+
**算法流程如下:**
35+
36+
* 新建两个 `HashSet` 集合,分别 `set1``set2`
37+
* 遍历数组 nums1,将元素添加到 `set1`
38+
* 遍历数组 nums2, 如果元素包含在 `set1` 中,将该元素添加在 `set2`
39+
* 最后集合 `set2` 就是我们需要的不重复的结果
40+
41+
42+
**复杂度分析:**
43+
44+
* 时间复杂度:0(m + n) ,m 是数组 nums1 的长度,n 是数组 nums2 的长度,遍历两个数组需要的时间复杂度为 0(m + n)
45+
* 空间复杂度:0(m + n),m 是数组 nums1 的长度,n 是数组 nums2 的长度,需要两个集合存储两个数组中的元素,所以空间复杂度取决于两个集合,即空间复杂度为 0(m + n)
46+
47+
### Java 实现
48+
49+
```
50+
class Solution {
51+
public int[] intersection(int[] nums1, int[] nums2) {
52+
53+
Set<Integer> set1 = new HashSet<>();
54+
for (int value : nums1) {
55+
set1.add(value);
56+
}
57+
58+
Set<Integer> set2 = new HashSet<Integer>();
59+
for (int value : nums2) {
60+
if (set1.contains(value)) {
61+
set2.add(value);
62+
}
63+
}
64+
65+
int[] result = new int[set2.size()];
66+
int index = 0;
67+
for (int value : set2) {
68+
result[index++] = value;
69+
}
70+
return result;
71+
}
72+
}
73+
```
74+
75+
### Kotlin 实现
76+
77+
78+
```
79+
class Solution {
80+
fun intersection(nums1: IntArray, nums2: IntArray): IntArray {
81+
val set1 = HashSet<Int>();
82+
for (value in nums1) {
83+
set1.add(value);
84+
}
85+
86+
val set2 = HashSet<Int>();
87+
for (value in nums2) {
88+
if (set1.contains(value)) {
89+
set2.add(value);
90+
}
91+
}
92+
93+
val result = IntArray(set2.size);
94+
var index = 0;
95+
for (value in set2) {
96+
result[index++] = value;
97+
}
98+
99+
return result;
100+
}
101+
}
102+
```
103+
104+

‎leetcode/menu.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,5 +59,9 @@
5959

6060
* [回文数](/math/01-palindrome-number.md)
6161

62+
* 哈希表
63+
64+
* [两个数组的交集](/hash/01-intersection-of-two-arrays.md)
65+
6266
[filename](../common/index_footer.md ':include')
6367

‎offer/common/index_header.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
<p align="center"> 仓库 <b>每周</b> 持续更新,如果对你有帮助,请在右上角 star 一下</p>
44

5-
<p align="center"> LeetCode 的题解进度,目前已经 AC 了 132 题</p>
5+
<p align="center"> LeetCode 的题解进度,目前已经 AC 了 133 题</p>
66

77
<p align="center"> 每题都有解题思路、时间复杂度、空间复杂度、源代码 </p>
88

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /