Skip to content

Commit

Permalink
添加 1002.查找常用字符 GO版本
Browse files Browse the repository at this point in the history
添加 1002.查找常用字符 GO版本
  • Loading branch information
X-shuffle authored Aug 19, 2021
1 parent 6afccb6 commit bf92f95
Showing 1 changed file with 35 additions and 1 deletion.
36 changes: 35 additions & 1 deletion problems/1002.查找常用字符.md
Original file line number Diff line number Diff line change
Expand Up @@ -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;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)
* B站视频:[代码随想录](https://space.bilibili.com/525438321)
Expand Down

0 comments on commit bf92f95

Please sign in to comment.