|
1 | | -#349. Intersection of Two Arrays - Best Practices of LeetCode Solutions |
2 | | -LeetCode link:[349. Intersection of Two Arrays](https://leetcode.com/problems/intersection-of-two-arrays), |
3 | | -[349. 两个数组的交集](https://leetcode.cn/problems/intersection-of-two-arrays) |
| 1 | +#349. 两个数组的交集 - 力扣题解最佳实践 |
| 2 | +力扣链接:[349. 两个数组的交集](https://leetcode.cn/problems/intersection-of-two-arrays) ,难度:**简单**。 |
4 | 3 |
|
5 | | -[中文题解](#中文题解) |
| 4 | +##力扣“349. 两个数组的交集”问题描述 |
| 5 | +给定两个数组`nums1` 和`nums2` ,返回_它们的**交集**_ 。输出结果中的每个元素一定是**唯一** 的。我们可以**不考虑输出结果的顺序**。 |
6 | 6 |
|
7 | | -##LeetCode problem description |
8 | | -Given two integer arrays`nums1` and`nums2`, return_an array of their**intersection**_. |
9 | | -Each element in the result must be**unique** and you may return the result in**any order**. |
| 7 | +>数组的交集: The intersection of two arrays is defined as the set of elements that are present in both arrays. |
10 | 8 |
|
11 | | -Difficulty:**Easy** |
| 9 | +###[示例 1] |
| 10 | + |
| 11 | +**输入**:`nums1 = [1,2,2,1], nums2 = [2,2]` |
| 12 | + |
| 13 | +**输出**:`[2]` |
12 | 14 |
|
13 | | -###[Example 1] |
14 | | -**Input**:`nums1 = [1,2,2,1], nums2 = [2,2]` |
| 15 | +###[示例 2] |
15 | 16 |
|
16 | | -**Output**:`[2]` |
| 17 | +**输入**:`nums1 = [4,9,5], nums2 = [9,4,9,8,4]` |
17 | 18 |
|
18 | | -###[Example 2] |
19 | | -**Input**:`nums1 = [4,9,5], nums2 = [9,4,9,8,4]` |
| 19 | +**输出**:`[9,4]` |
20 | 20 |
|
21 | | -**Output**:`[9,4]` or`[4,9]` |
| 21 | +**解释**:`[4,9] 也是可通过的` |
22 | 22 |
|
23 | | -###[Constraints] |
| 23 | +###[约束] |
24 | 24 | -`1 <= nums1.length, nums2.length <= 1000` |
25 | 25 | -`0 <= nums1[i], nums2[i] <= 1000` |
26 | 26 |
|
27 | | -##Intuition |
28 | | -[中文题解](#中文题解) |
29 | | - |
30 | | -1. Convert one of the arrays to a`set`. The elements are unique in a`set`. |
31 | | -2. When traversing the other array, if the an element is found to already exist in the`set`, it means that the element belongs to the intersection, and the element should be added to the`results`. |
32 | | -3. The`results` is also of`set` type because duplicate removal is required. |
| 27 | +##思路 |
| 28 | +1. 把其中一个数组转为`set`,数据结构`set`的特点是元素不重复。 |
| 29 | +2. 遍历另一个数组时,如果发现当前元素已经存在于`set`中,则说明该元素属于交集,将该元素加入结果集中。 |
| 30 | +3. 结果集也采用`set`类型,因为需要去重。 |
33 | 31 |
|
34 | | -##Complexity |
35 | | -*Time:`O(n)`. |
36 | | -*Space:`O(n)`. |
| 32 | +##复杂度 |
| 33 | +*时间:`O(N)`。 |
| 34 | +*空间:`O(N)`。 |
37 | 35 |
|
38 | 36 | ##Java |
39 | 37 | ```java |
@@ -167,48 +165,7 @@ def intersection(nums1, nums2) |
167 | 165 | end |
168 | 166 | ``` |
169 | 167 |
|
170 | | -##C |
171 | | -```c |
172 | | -// Welcome to create a PR to complete the code of this language, thanks! |
173 | | -``` |
174 | | - |
175 | | -##Kotlin |
176 | | -```kotlin |
177 | | -// Welcome to create a PR to complete the code of this language, thanks! |
178 | | -``` |
179 | | - |
180 | | -##Swift |
181 | | -```swift |
182 | | -// Welcome to create a PR to complete the code of this language, thanks! |
183 | | -``` |
184 | | - |
185 | | -##Rust |
186 | | -```rust |
187 | | -// Welcome to create a PR to complete the code of this language, thanks! |
188 | | -``` |
189 | | - |
190 | | -##Other languages |
| 168 | +##C, Kotlin, Swift, Rust or other languages |
191 | 169 | ``` |
192 | 170 | // Welcome to create a PR to complete the code of this language, thanks! |
193 | 171 | ``` |
194 | | - |
195 | | -##问题描述 |
196 | | -给定两个数组`nums1` 和`nums2` ,返回_它们的**交集**_ 。输出结果中的每个元素一定是**唯一** 的。我们可以**不考虑输出结果的顺序** 。 |
197 | | - |
198 | | -难度:**容易** |
199 | | - |
200 | | -###[示例 1] |
201 | | -**输入**:`nums1 = [1,2,2,1], nums2 = [2,2]` |
202 | | - |
203 | | -**输出**:`[2]` |
204 | | - |
205 | | -###[示例 2] |
206 | | -**输入**:`nums1 = [4,9,5], nums2 = [9,4,9,8,4]` |
207 | | - |
208 | | -**输出**:`[9,4]` 或者`[4,9]` |
209 | | - |
210 | | -#中文题解 |
211 | | -##思路 |
212 | | -1. 把其中一个数组转为`set`,数据结构`set`的特点是元素不重复。 |
213 | | -2. 遍历另一个数组时,如果发现当前元素已经存在于`set`中,则说明该元素属于交集,将该元素加入结果集中。 |
214 | | -3. 结果集也采用`set`类型,因为需要去重。 |