收废品做网站,wordpress4.9.8有中文版,做网站编程用什么语言好,网站集约化建设优势1. 题目
在老式手机上#xff0c;用户通过数字键盘输入#xff0c;手机将提供与这些数字相匹配的单词列表。 每个数字映射到0至4个字母。给定一个数字序列#xff0c;实现一个算法来返回匹配单词的列表。 你会得到一张含有有效单词的列表。映射如下图所示#xff1a;
示…1. 题目
在老式手机上用户通过数字键盘输入手机将提供与这些数字相匹配的单词列表。 每个数字映射到0至4个字母。给定一个数字序列实现一个算法来返回匹配单词的列表。 你会得到一张含有有效单词的列表。映射如下图所示
示例 1:
输入: num 8733, words [tree, used]
输出: [tree, used]示例 2:
输入: num 2, words [a, b, c, d]
输出: [a, b, c]提示
num.length 1000
words.length 500
words[i].length num.length
num中不会出现 0, 1 这两个数字来源力扣LeetCode 链接https://leetcode-cn.com/problems/t9-lcci 著作权归领扣网络所有。商业转载请联系官方授权非商业转载请注明出处。
2. 解题
类似题目LeetCode 17. 电话号码的字母组合回溯
class Solution {
public:vectorstring getValidT9Words(string num, vectorstring words) {string key[10] {,,abc,def,ghi,jkl,mno,pqrs,tuv,wxyz};vectorstring ans;int i;bool ok;for(auto w : words){ok true;for(i 0; i w.size(); i){if(key[num[i]-0].find(w[i]) string::npos){ok false;break;}}if(ok)ans.push_back(w);}return ans;}
};36 ms 12.2 MB
或者比较数字是否相等
class Solution {
public:vectorstring getValidT9Words(string num, vectorstring words) {char ch[26] {2, 2, 2, 3, 3, 3, 4, 4, 4,5, 5, 5, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 9, 9, 9, 9};vectorstring ans;int i;bool ok;for(auto w : words){ok true;for(i 0; i w.size(); i){if(num[i] ! ch[w[i]-a]){ok false;break;}}if(ok)ans.push_back(w);}return ans;}
};28 ms 12 MB