当前位置:网站首页>[Golang]力扣Leetcode - 349. 两个数组的交集(哈希表)
[Golang]力扣Leetcode - 349. 两个数组的交集(哈希表)
2022-06-11 17:59:00 【跳舞的皮埃尔】
[Golang]力扣Leetcode - 349. 两个数组的交集(哈希表)
题目:给定两个数组 nums1 和 nums2 ,返回 它们的交集 。输出结果中的每个元素一定是 唯一 的。我们可以 不考虑输出结果的顺序 。
链接: 力扣Leetcode - 349. 两个数组的交集.
示例1:
输入:nums1 = [1,2,2,1], nums2 = [2,2]
输出:[2]
示例 2:
输入:nums1 = [4,9,5], nums2 = [9,4,9,8,4]
输出:[9,4]
解释:[4,9] 也是可通过的
思路: 我的想法是,把 nums1 和 nums2 用哈希表 a 和 b 表示,然后再遍历 a 和 b,如果 a 中 key1 等于 key2,就把该 key 添加到输出 res 中,并且跳出该循环,这样好处是输出 res 不会出现重复的数。
Go代码:
package main
import "fmt"
func intersection(nums1 []int, nums2 []int) []int {
// 用一个map来存储数字以及出现的次数
a := make(map[int]int)
b := make(map[int]int)
for _, v := range nums1 {
a[v]++
}
for _, v := range nums2 {
b[v]++
}
// 定义存储最后结果的数组
var res []int
for k1 := range a {
for k2 := range b {
if k1 == k2 {
res = append(res, k1)
break
}
}
}
return res
}
func main() {
nums1 := []int{
1, 2, 2, 1}
nums2 := []int{
2, 2}
fmt.Print(intersection(nums1, nums2))
}
提交截图:
边栏推荐
- Initial experience of MariaDB spider sharding engine
- Mysql8 installation, Navicat installation, sqli labs setup
- SISO Decoder for min-sum(补充章节2)
- PIL-Pillow图像处理【1】-安装与新建
- LDPC 7 - 解码简单例子
- Experiment 3: design and verify all operations represented by linear table sequence on the computer
- Dynamic: capturing network dynamics using dynamic graph representation learning
- 6-3 reading articles (*)
- Sword finger offer (2nd Edition)
- Cryptology Summary
猜你喜欢

mariadb spider分片引擎初体验

New work of "the father of LSTM": a new method towards self correcting neural network

How ZABBIX can customize MySQL monitoring items and trigger alarms

Seeing the sudden death of a 28 year old employee, I was silent

【无标题】
![Spring 2021 daily question [end of week4]](/img/b3/2f5a66b0d4374db3d4db0b71d72f7e.jpg)
Spring 2021 daily question [end of week4]

Tle6288r is a 6-channel (150 MOhm) intelligent multi-channel switch using intelligent power technology - keshijin mall

【C】 Compilation preprocessing and environment

ctfhub-sql布尔盲注

Understanding of distributed transactions
随机推荐
ACL 2022:评估单词多义性不再困扰?一种新的基准“DIBIMT”
【无标题】
Sqli labs customs clearance hey hey~
Database lock and transaction isolation level
送给大模型的「高考」卷:442人联名论文给大模型提出204个任务,谷歌领衔
Tle6288r is a 6-channel (150 MOhm) intelligent multi-channel switch using intelligent power technology - keshijin mall
Secret comment-----
After class, I looked at the document and went back to the lab. I picked up the forgotten SQL operators again
6-8 reading and writing of structured files 1
Ffmpeg parity field frame interlace progressive command and code processing
Talking about telework | community essay solicitation
[document operation] of ACM
了解一下random库·1
[collect first and use it sooner or later] 100 Flink high-frequency interview questions series (III)
[pat grade B question bank] complete summary
"College entrance examination" volume sent to the big model: 442 people put forward 204 tasks to the big model, led by Google
LDPC 7 - 解码简单例子
Initial egg framework
網絡安全威脅情報體系
Sa-Token 单点登录 SSO模式二 URL重定向传播会话示例