mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-06 15:09:40 +08:00
更新 0349.两个数组的交集 排版格式修复
This commit is contained in:
@ -10,7 +10,7 @@
|
||||
> 如果哈希值比较少、特别分散、跨度非常大,使用数组就造成空间的极大浪费!
|
||||
|
||||
|
||||
## 349. 两个数组的交集
|
||||
# 349. 两个数组的交集
|
||||
|
||||
[力扣题目链接](https://leetcode.cn/problems/intersection-of-two-arrays/)
|
||||
|
||||
@ -22,9 +22,11 @@
|
||||
输出结果中的每个元素一定是唯一的。
|
||||
我们可以不考虑输出结果的顺序。
|
||||
|
||||
## 思路
|
||||
## 算法公开课
|
||||
|
||||
关于本题,我录制了讲解视频:[学透哈希表,set使用有技巧!Leetcode:349. 两个数组的交集](https://www.bilibili.com/video/BV1ba411S7wu),看视频配合题解,事半功倍。
|
||||
**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html)::[学透哈希表,set使用有技巧!Leetcode:349. 两个数组的交集](https://www.bilibili.com/video/BV1ba411S7wu),相信结合视频再看本篇题解,更有助于大家对本题的理解**。
|
||||
|
||||
## 思路
|
||||
|
||||
这道题目,主要要学会使用一种哈希数据结构:unordered_set,这个数据结构可以解决很多类似的问题。
|
||||
|
||||
@ -118,8 +120,7 @@ public:
|
||||
|
||||
## 其他语言版本
|
||||
|
||||
|
||||
Java:
|
||||
### Java:
|
||||
|
||||
```Java
|
||||
import java.util.HashSet;
|
||||
@ -159,8 +160,9 @@ class Solution {
|
||||
}
|
||||
```
|
||||
|
||||
Python3:
|
||||
### Python3:
|
||||
(版本一) 使用字典和集合
|
||||
|
||||
```python
|
||||
class Solution:
|
||||
def intersection(self, nums1: List[int], nums2: List[int]) -> List[int]:
|
||||
@ -206,7 +208,8 @@ class Solution:
|
||||
|
||||
```
|
||||
|
||||
Go:
|
||||
### Go:
|
||||
|
||||
```go
|
||||
func intersection(nums1 []int, nums2 []int) []int {
|
||||
set:=make(map[int]struct{},0) // 用map模拟set
|
||||
@ -227,7 +230,7 @@ func intersection(nums1 []int, nums2 []int) []int {
|
||||
}
|
||||
```
|
||||
|
||||
javaScript:
|
||||
### JavaScript:
|
||||
|
||||
```js
|
||||
/**
|
||||
@ -255,7 +258,7 @@ var intersection = function(nums1, nums2) {
|
||||
};
|
||||
```
|
||||
|
||||
TypeScript:
|
||||
### TypeScript:
|
||||
|
||||
版本一(正常解法):
|
||||
|
||||
@ -280,7 +283,7 @@ function intersection(nums1: number[], nums2: number[]): number[] {
|
||||
};
|
||||
```
|
||||
|
||||
Swift:
|
||||
### Swift:
|
||||
|
||||
```swift
|
||||
func intersection(_ nums1: [Int], _ nums2: [Int]) -> [Int] {
|
||||
@ -298,7 +301,8 @@ func intersection(_ nums1: [Int], _ nums2: [Int]) -> [Int] {
|
||||
}
|
||||
```
|
||||
|
||||
PHP:
|
||||
### PHP:
|
||||
|
||||
```php
|
||||
class Solution {
|
||||
/**
|
||||
@ -327,7 +331,8 @@ class Solution {
|
||||
}
|
||||
```
|
||||
|
||||
Rust:
|
||||
### Rust:
|
||||
|
||||
```rust
|
||||
use std::collections::HashSet;
|
||||
impl Solution {
|
||||
@ -363,7 +368,8 @@ impl Solution {
|
||||
}
|
||||
```
|
||||
|
||||
C:
|
||||
### C:
|
||||
|
||||
```C
|
||||
int* intersection1(int* nums1, int nums1Size, int* nums2, int nums2Size, int* returnSize){
|
||||
|
||||
@ -394,7 +400,7 @@ int* intersection1(int* nums1, int nums1Size, int* nums2, int nums2Size, int* re
|
||||
}
|
||||
```
|
||||
|
||||
Scala:
|
||||
### Scala:
|
||||
|
||||
正常解法:
|
||||
```scala
|
||||
@ -439,8 +445,8 @@ object Solution {
|
||||
|
||||
```
|
||||
|
||||
### C#:
|
||||
|
||||
C#:
|
||||
```csharp
|
||||
public int[] Intersection(int[] nums1, int[] nums2) {
|
||||
if(nums1==null||nums1.Length==0||nums2==null||nums1.Length==0)
|
||||
@ -461,11 +467,10 @@ C#:
|
||||
```
|
||||
## 相关题目
|
||||
|
||||
* 350.两个数组的交集 II
|
||||
* [350.两个数组的交集 II](https://leetcode.cn/problems/intersection-of-two-arrays-ii/)
|
||||
|
||||
|
||||
<p align="center">
|
||||
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
|
||||
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
|
||||
</a>
|
||||
|
||||
|
Reference in New Issue
Block a user