【数组】面试题 16.20. T9键盘

题目:

 

 

 

解答:

 

 

 1 class Solution {
 2 public:
 3     vector<string> getValidT9Words(string num, vector<string>& words) 
 4     {
 5         vector<string> svec;
 6         string letters[10] = {"","","abc","def","ghi","jkl","mno","pqrs","tuv","wxyz"};
 7         int count = 0;
 8         string zfc;
 9         for(int i = 0; i < words.size(); i++)
10         {                
11             // adm    deo     vam  fan
12             for(int j = 0; j < words[i].size(); j++)
13             {        
14                 // a d m
15                 zfc = letters[num[j]-'0'];
16                 if(zfc.find(words[i][j]) != zfc.npos) 
17                 {
18                     count++;        // 如果找得到
19                 }
20             }
21             if(count == words[i].size()) 
22             {
23                 svec.push_back(words[i]); 
24             }
25             count = 0;
26         }
27         
28         return svec;
29     }
30 };

 

posted @ 2020-05-05 20:29  梦醒潇湘  阅读(174)  评论(0)    收藏  举报