mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-09 19:44:45 +08:00
@ -143,6 +143,26 @@ func intersection(nums1 []int, nums2 []int) []int {
|
|||||||
return res
|
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:
|
javaScript:
|
||||||
|
|
||||||
|
@ -233,7 +233,41 @@ var commonChars = function (words) {
|
|||||||
return res
|
return res
|
||||||
};
|
};
|
||||||
```
|
```
|
||||||
|
GO
|
||||||
|
```golang
|
||||||
|
func commonChars(words []string) []string {
|
||||||
|
length:=len(words)
|
||||||
|
fre:=make([][]int,0)//统计每个字符串的词频
|
||||||
|
res:=make([]string,0)
|
||||||
|
//统计词频
|
||||||
|
for i:=0;i<length;i++{
|
||||||
|
var row [26]int//存放该字符串的词频
|
||||||
|
for j:=0;j<len(words[i]);j++{
|
||||||
|
row[words[i][j]-97]++
|
||||||
|
}
|
||||||
|
fre=append(fre,row[:])
|
||||||
|
}
|
||||||
|
//查找一列的最小值
|
||||||
|
for j:=0;j<len(fre[0]);j++{
|
||||||
|
pre:=fre[0][j]
|
||||||
|
for i:=0;i<len(fre);i++{
|
||||||
|
pre=min(pre,fre[i][j])
|
||||||
|
}
|
||||||
|
//将该字符添加到结果集(按照次数)
|
||||||
|
tmpString:=string(j+97)
|
||||||
|
for i:=0;i<pre;i++{
|
||||||
|
res=append(res,tmpString)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return res
|
||||||
|
}
|
||||||
|
func min(a,b int)int{
|
||||||
|
if a>b{
|
||||||
|
return b
|
||||||
|
}
|
||||||
|
return a
|
||||||
|
}
|
||||||
|
```
|
||||||
-----------------------
|
-----------------------
|
||||||
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
|
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
|
||||||
* B站视频:[代码随想录](https://space.bilibili.com/525438321)
|
* B站视频:[代码随想录](https://space.bilibili.com/525438321)
|
||||||
|
Reference in New Issue
Block a user