Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit bf92f95

Browse files
添加 1002.查找常用字符 GO版本
添加 1002.查找常用字符 GO版本
1 parent 6afccb6 commit bf92f95

File tree

1 file changed

+35
-1
lines changed

1 file changed

+35
-1
lines changed

‎problems/1002.查找常用字符.md‎

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,41 @@ var commonChars = function (words) {
233233
return res
234234
};
235235
```
236-
236+
GO
237+
```golang
238+
func commonChars(words []string) []string {
239+
length:=len(words)
240+
fre:=make([][]int,0)//统计每个字符串的词频
241+
res:=make([]string,0)
242+
//统计词频
243+
for i:=0;i<length;i++{
244+
var row [26]int//存放该字符串的词频
245+
for j:=0;j<len(words[i]);j++{
246+
row[words[i][j]-97]++
247+
}
248+
fre=append(fre,row[:])
249+
}
250+
//查找一列的最小值
251+
for j:=0;j<len(fre[0]);j++{
252+
pre:=fre[0][j]
253+
for i:=0;i<len(fre);i++{
254+
pre=min(pre,fre[i][j])
255+
}
256+
//将该字符添加到结果集(按照次数)
257+
tmpString:=string(j+97)
258+
for i:=0;i<pre;i++{
259+
res=append(res,tmpString)
260+
}
261+
}
262+
return res
263+
}
264+
func min(a,b int)int{
265+
if a>b{
266+
return b
267+
}
268+
return a
269+
}
270+
```
237271
-----------------------
238272
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
239273
* B站视频:[代码随想录](https://space.bilibili.com/525438321)

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /