方法一
思路:使用一个map,key为字符,val为对应的行号
func findWords(words []string) []string {hmap := map[byte]int{'A': 1,'B': 2,'C': 2,'D': 1,'E': 0,'F': 1,'G': 1,'H': 1,'I': 0,'J': 1,'K': 1,'L': 1,'M': 2,'N': 2,'O': 0,'P': 0,'Q': 0,'R': 0,'S': 1,'T': 0,'U': 0,'V': 2,'W': 0,'X': 2,'Y': 0,'Z': 2,}var flag boolret := []string{}for _, word := range words {start := hmap[byte(unicode.ToUpper(rune(word[0])))]flag = truefor _, ch := range word {if start != hmap[byte(unicode.ToUpper(ch))] {flag = falsebreak}}if flag {ret = append(ret, word)}}for _,v := range hmap {fmt.Printf("%d", v)}return ret}
【推荐】方法二
思路:使用一个数组存储每一个字母的行号,不需要使用hmap
�
func findWords(words []string) []string {
//二十六个字母在的行号
rowLines := "12210111011122000010020202"
var flag bool
ret := []string{}
for _, word := range words {
start := rowLines[unicode.ToUpper(rune(word[0])) - 'A']
flag = true
for _, ch := range word {
if start != rowLines[unicode.ToUpper(ch) - 'A'] {
flag = false
break
}
}
if flag {
ret = append(ret, word)
}
}
return ret
}
