栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

前往:如果唯一则追加

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

前往:如果唯一则追加

您的方法将为每次插入花费线性时间。更好的方法是使用

map[int]struct{}
。另外,您也可以使用a
map[int]bool
或类似名称,但是empty
struct{}
的优点是它不占用任何额外空间。因此,
map[int]struct{}
是一组整数的普遍选择。

例:

set := make(map[int]struct{})set[1] = struct{}{}set[2] = struct{}{}set[1] = struct{}{}// ...for key := range(set) {  fmt.Println(key)}// each value will be printed only once, in no particular order// you can use the ,ok idiom to check for existing keysif _, ok := set[1]; ok {  fmt.Println("element found")} else {  fmt.Println("element not found")}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/469030.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号