class Solution {
public:
vector<string> findWords(vector<string>& words) {
}
};
500. 键盘行
给你一个字符串数组 words
,只返回可以使用在 美式键盘 同一行的字母打印出来的单词。键盘如下图所示。
美式键盘 中:
"qwertyuiop"
组成。"asdfghjkl"
组成。"zxcvbnm"
组成。
示例 1:
输入:words = ["Hello","Alaska","Dad","Peace"] 输出:["Alaska","Dad"]
示例 2:
输入:words = ["omk"] 输出:[]
示例 3:
输入:words = ["adsdf","sfd"] 输出:["adsdf","sfd"]
提示:
1 <= words.length <= 20
1 <= words[i].length <= 100
words[i]
由英文字母(小写和大写字母)组成原站题解
golang 解法, 执行用时: 0 ms, 内存消耗: 1.9 MB, 提交时间: 2021-06-10 18:05:23
func findWords(words []string) (ans []string) { kb := []string{"qwertyuiop", "asdfghjkl", "zxcvbnm"} mp := make(map[byte]int) for i, k := range kb { for j := range k { mp[k[j]] = i mp[k[j]-32] = i } } for _, word := range words { line, _ := mp[word[0]] same := true for j := 1; j < len(word); j++ { if l, _ := mp[word[j]]; l != line { same = false break } } if same { ans = append(ans, word) } } return }