mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-09 11:34:46 +08:00
添加 1002.查找常用字符 GO版本
添加 1002.查找常用字符 GO版本
This commit is contained in:
@ -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