mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-09 03:34:02 +08:00
Merge pull request #1015 from xiaofei-2020/hash3
添加(0349.两个数组的交集.md):增加typescript版本
This commit is contained in:
@ -190,7 +190,33 @@ var intersection = function(nums1, nums2) {
|
|||||||
};
|
};
|
||||||
```
|
```
|
||||||
|
|
||||||
|
TypeScript:
|
||||||
|
|
||||||
|
版本一(正常解法):
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
function intersection(nums1: number[], nums2: number[]): number[] {
|
||||||
|
let resSet: Set<number> = new Set(),
|
||||||
|
nums1Set: Set<number> = new Set(nums1);
|
||||||
|
for (let i of nums2) {
|
||||||
|
if (nums1Set.has(i)) {
|
||||||
|
resSet.add(i);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return Array.from(resSet);
|
||||||
|
};
|
||||||
|
```
|
||||||
|
|
||||||
|
版本二(秀操作):
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
function intersection(nums1: number[], nums2: number[]): number[] {
|
||||||
|
return Array.from(new Set(nums1.filter(i => nums2.includes(i))))
|
||||||
|
};
|
||||||
|
```
|
||||||
|
|
||||||
Swift:
|
Swift:
|
||||||
|
|
||||||
```swift
|
```swift
|
||||||
func intersection(_ nums1: [Int], _ nums2: [Int]) -> [Int] {
|
func intersection(_ nums1: [Int], _ nums2: [Int]) -> [Int] {
|
||||||
var set1 = Set<Int>()
|
var set1 = Set<Int>()
|
||||||
|
Reference in New Issue
Block a user