class Solution:
def getValidT9Words(self, num, words):
# 从数字序列推字符序列的组合是1->N
# 但是从每个字符的数字是一定的
# 将字符映射为数字 然后看是否来自当前数字组合即可
res = []
Reflect = {
"a":"2","b":"2","c":"2","d":"3","e":"3","f":"3","g":"4",
"h":"4","i":"4","j":"5","k":"5","l":"5","m":"6","n":"6",
"o":"6","p":"7","q":"7", "r":"7","s":"7","t":"8",
"u":"8","v":"8","w":"9", "x":"9","y":"9","z":"9",
}
for word in words:
numList = []
for char in word:
numList.append(Reflect[char])
reconstruction = "".join(numList)
if reconstruction == num:
res.append(word)
return res
作者:dustw1nd
链接:https://leetcode-cn.com/problems/t9-lcci/solution/shu-zi-zi-fu-chuan-1-n-zi-fu-chuan-shu-z-yj02/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
leetcode刷题250天(11)——面试题 16.20. T9键盘
最新推荐文章于 2024-08-06 17:50:14 发布