LeetCode 键盘行

题目传送
在这里插入图片描述

直接预处理一下,直接暴力跑就可以了,就是如何简化过程的问题了。先把26个字母预分类一下。后面直接好用。
AC代码

class Solution {
    
    
public:
    vector<string> findWords(vector<string>& words) {
    
    
    vector<string> v;
    string rowIdx = "12210111011122000010020202"; //预处理
    for(int i = 0;i < words.size();i++){
    
    
        string a = words[i];
        int ans = -2;
        for(int j = 0;j < a.size();j++){
    
     //大小写字母转化
            if(a[j] >= 'A' && a[j] <= 'Z'){
    
    
                a[j] += 32;
            }
            int b = a[j] - 'a';
            if(ans == -2){
    
    
                ans = rowIdx[b];
            }
            else if(ans != rowIdx[b]){
    
    
                ans = -1;
                break;
            }
        }
        if(ans != -1){
    
    
            v.push_back(words[i]);
        }
    }
    return v;
    }
};

猜你喜欢

转载自blog.csdn.net/moasad/article/details/121064832