mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-08 08:50:15 +08:00
Merge pull request #2422 from tlylt/update-0349-go
Update 0349 - Add Go solution
This commit is contained in:
@ -231,6 +231,8 @@ class Solution:
|
|||||||
|
|
||||||
### Go:
|
### Go:
|
||||||
|
|
||||||
|
(版本一)使用字典和集合
|
||||||
|
|
||||||
```go
|
```go
|
||||||
func intersection(nums1 []int, nums2 []int) []int {
|
func intersection(nums1 []int, nums2 []int) []int {
|
||||||
set:=make(map[int]struct{},0) // 用map模拟set
|
set:=make(map[int]struct{},0) // 用map模拟set
|
||||||
@ -251,6 +253,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:
|
### JavaScript:
|
||||||
|
|
||||||
```js
|
```js
|
||||||
|
Reference in New Issue
Block a user