Merge pull request #2422 from tlylt/update-0349-go

Update 0349 - Add Go solution
This commit is contained in:
程序员Carl
2024-02-04 14:55:40 +08:00
committed by GitHub

View File

@ -231,6 +231,8 @@ class Solution:
### Go
(版本一)使用字典和集合
```go
func intersection(nums1 []int, nums2 []int) []int {
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:
```js