Re: [闲聊] 每日leetcode

楼主: JIWP (JIWP)   2024-06-05 18:58:59
1002. Find Common Characters
给一个string array
请回传在每个string都出现过的字符
思路:
用两个矩阵
rec纪录到目前为止出现过的字符次数
tmp纪录目前这个string字符出现的次数
当rec[i]>tmp[i],tmp[i]++
golang code:
func commonChars(words []string) []string {
rec:=[26]int{}
res:=[]string{}
for _,val:=range words[0]{
rec[val-'a']++
}
for _,val:=range words{
tmp:=[26]int{}
for _,char:=range val{
if rec[char-'a']>tmp[char-'a']{
tmp[char-'a']++
}
}
rec=tmp
}
for key,val:=range rec{
for i:=0;i<val;i++{
res=append(res,string('a'+key))
}
}
return res
}

Links booklink

Contact Us: admin [ a t ] ucptt.com