diff --git a/problems/0349.两个数组的交集.md b/problems/0349.两个数组的交集.md index 7489352d..62abf639 100644 --- a/problems/0349.两个数组的交集.md +++ b/problems/0349.两个数组的交集.md @@ -143,6 +143,26 @@ func intersection(nums1 []int, nums2 []int) []int { return res } ``` +```golang +//优化版,利用set,减少count统计 +func intersection(nums1 []int, nums2 []int) []int { + set:=make(map[int]struct{},0) + res:=make([]int,0) + for _,v:=range nums1{ + if _,ok:=set[v];!ok{ + set[v]=struct{}{} + } + } + for _,v:=range nums2{ + //如果存在于上一个数组中,则加入结果集,并清空该set值 + if _,ok:=set[v];ok{ + res=append(res,v) + delete(set, v) + } + } + return res +} +``` javaScript: diff --git a/problems/1002.查找常用字符.md b/problems/1002.查找常用字符.md index 723e5656..a789373b 100644 --- a/problems/1002.查找常用字符.md +++ b/problems/1002.查找常用字符.md @@ -233,7 +233,41 @@ var commonChars = function (words) { return res }; ``` - +GO +```golang +func commonChars(words []string) []string { + length:=len(words) + fre:=make([][]int,0)//统计每个字符串的词频 + res:=make([]string,0) + //统计词频 + for i:=0;ib{ + return b + } + return a +} +``` ----------------------- * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) * B站视频:[代码随想录](https://space.bilibili.com/525438321)