分享
  1. 首页
  2. 文章

【Golang语言】LeetCode 1002. Find Common Characters

努力的C · · 1098 次点击 · · 开始浏览
这是一个创建于 的文章,其中的信息可能已经有所发展或是发生改变。

Given an array A of strings made only from lowercase letters, return a list of all characters that show up in all strings within the list (including duplicates). For example, if a character occurs 3 times in all strings but not 4 times, you need to include that character three times in the final answer.

给定仅有小写字母组成的字符串数组 A,返回列表中的每个字符串中都显示的全部字符(包括重复字符)组成的列表。例如,如果一个字符在每个字符串中出现 3 次,但不是 4 次,则需要在最终答案中包含该字符 3 次。
你可以按任意顺序返回答案。
https://leetcode-cn.com/problems/find-common-characters

Input: ["bella","label","roller"]
Output: ["e","l","l"]
Input: ["cool","lock","cook"]
Output: ["c","o"]

题意:找出所有字符串中都出现过的字符,其实就是求各个字符串的交集,可以按任意顺序返回。
思路一:求每次字符串中的字符出现次数,每次两两字符串比较,26个字符取最小次数的则为两字符串的交集,比如
bella b->1 e->1 l->2 a->1
label b->1 e->1 l->2 a->1 所以这俩字符串的交集就是a b e l l
然后继续往下遍历即可。
用value-'a' 表示26个字母 最后再 value+'a' 转回来


func commonChars(A []string) []string {
 result := make([]int, 26)
 for _, value := range A[0] {
 result[value-'a']++
 }
 for i := 1; i < len(A); i++ {
 temp := make([]rune, 26)
 for _, value := range A[i] {
 temp[value-'a']++
 }
 for j := 0; j < 26; j++ {
 result[j] = int(math.Min(float64(temp[j]), float64(result[j])))
 }
 }
 ret := make([]string, 0)
 for i := 0; i < 26; i++ {
 if result[i] > 0 {
 times := result[i]
 j := 0
 for j < times {
 ret = append(ret, string(i+'a'))
 j++
 }
 }
 }
 return ret
}

思路二:其实也是差不多,也是直接两两比较,求交集,比较到最后的结果就是最后的结果。不过这里的求交集方法不太一样。 由于要考虑到重复出现的字符,所以需要采用一个数组来记录某个字符最近一次被找到的位置。如果再次遇到该字符,那么将会从该位置后面开始寻找。

func commonChars1002(A []string) []string {
 result := make([]string, 0)
 if len(A) == 1 {
 for _, rune := range A[0] {
 result = append(result, string(rune))
 }
 return result
 }
 common := commonStr(A[0], A[1])
 for i := 2; i < len(A); i++ {
 common = commonStr(A[i], common)
 }
 for _, rune := range common {
 result = append(result, string(rune))
 }
 return result
}
func commonStr(a string, b string) string {
 indexMap := [26]int{}
 result := make([]rune, 0)
 index := 0
 for _, rune := range a {
 beforeIndex := indexMap[rune-'a']
 index = strings.Index(b[beforeIndex:], string(rune))
 if index < len(b) && index >= 0 {
 result = append(result, rune)
 indexMap[rune-'a'] = index + beforeIndex + 1//截取后索引会从0开始,所以得加上之前的
 }
 }
 return string(result)
}

有疑问加站长微信联系(非本文作者)

本文来自:51CTO博客

感谢作者:努力的C

查看原文:【Golang语言】LeetCode 1002. Find Common Characters

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

关注微信
1098 次点击
添加一条新回复 (您需要 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传

用户登录

没有账号?注册
(追記) (追記ここまで)

今日阅读排行

    加载中
(追記) (追記ここまで)

一周阅读排行

    加载中

关注我

  • 扫码关注领全套学习资料 关注微信公众号
  • 加入 QQ 群:
    • 192706294(已满)
    • 731990104(已满)
    • 798786647(已满)
    • 729884609(已满)
    • 977810755(已满)
    • 815126783(已满)
    • 812540095(已满)
    • 1006366459(已满)
    • 692541889

  • 关注微信公众号
  • 加入微信群:liuxiaoyan-s,备注入群
  • 也欢迎加入知识星球 Go粉丝们(免费)

给该专栏投稿 写篇新文章

每篇文章有总共有 5 次投稿机会

收入到我管理的专栏 新建专栏