力扣 LeetCode 面试题 16.20. T9键盘

力扣 LeetCode 面试题 16.20. T9键盘_第1张图片
用一个letters[10]记录手机字母,再把word[i]长度与num长度对比,如果一样,则利用atoi函数把num[j]变为数字a,去看letters[a]中是否含有word[i][j]

class Solution {
public:
    string letters[10] = {"","","abc","def","ghi","jkl","mno","pqrs","tuv","wxyz"};
    vector<string> getValidT9Words(string num, vector<string>& words) {
        vector<string>res;
        for(int i=0;i<words.size();i++){
            
            if(words[i].size()!=num.size()){
                continue;
            }
            int sig=0;
            for(int j=0;j<words[i].size();j++){
                string s;
                s+=num[j];
                if(letters[atoi(s.c_str())].find(words[i][j])==-1){
                    sig=1;
                    break;
                }
            }
            if(sig==0){
                res.push_back(words[i]);
            }
        }
        return res;
    }
};

你可能感兴趣的:(力扣题目,leetcode,c++)