C++ Fast Solution.


  • 1
    P
    class Solution {
    public:
        vector<string> findWords(vector<string>& words) {
            vector<int>r1(256);
            
            string s1 = "asdfghjklASDFGHJKL" ; 
            string s2 = "qwertyuiopQWERTYUIOP"; 
            string s3 = "zxcvbnmZXCVBNM" ;  
            
            for(const auto x : s1) r1[x] = 2;
            for(const auto x : s2) r1[x] = 3;
            for(const auto x : s3) r1[(x)] = 5;
            
            vector<string> result;
            
            for(const auto& w : words){
                int isSingle = 0;
                for(const auto &x : w){
                    if(!isSingle) isSingle = r1[x];
                    else{
                        if(r1[x] != isSingle){isSingle = -1; break;}
                    }
                }
                if(isSingle > 0) result.emplace_back(w);
            }
            return result;
        }
    };

  • 0
    Z

    Can I ask why you use parentheses at r1[(x)] = 5; I tried to use r1[x] = 5; int this solution, the speed decreased


  • 0
    P

    @zeming Actually that was a typo. Nothing really happens. Your speed probably decreased due to leetcode server load or CPU scheduling.


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.