mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-09 03:34:02 +08:00
Add two versions of intersection function in Go
This commit is contained in:
@ -210,6 +210,8 @@ class Solution:
|
||||
|
||||
### Go:
|
||||
|
||||
(版本一)使用字典和集合
|
||||
|
||||
```go
|
||||
func intersection(nums1 []int, nums2 []int) []int {
|
||||
set:=make(map[int]struct{},0) // 用map模拟set
|
||||
@ -230,6 +232,28 @@ func intersection(nums1 []int, nums2 []int) []int {
|
||||
}
|
||||
```
|
||||
|
||||
(版本二)使用数组
|
||||
|
||||
```go
|
||||
func intersection(nums1 []int, nums2 []int) []int {
|
||||
count1 := make([]int, 1001, 1001)
|
||||
count2 := make([]int, 1001, 1001)
|
||||
res := make([]int, 0)
|
||||
for _, v := range nums1 {
|
||||
count1[v] = 1
|
||||
}
|
||||
for _, v := range nums2 {
|
||||
count2[v] = 1
|
||||
}
|
||||
for i := 0; i <= 1000; i++ {
|
||||
if count1[i] + count2[i] == 2 {
|
||||
res = append(res, i)
|
||||
}
|
||||
}
|
||||
return res
|
||||
}
|
||||
```
|
||||
|
||||
### JavaScript:
|
||||
|
||||
```js
|
||||
|
Reference in New Issue
Block a user