beats 73.87% of cpp submissions


  • 0
    1
    class Solution {
    public:
        int numOfBit(int num)
        {
            int sum = 0;
            while(num)
            {
                sum++;
                num = num&(num-1);
            }
            return sum;
        }
        
        vector<string> findWords(vector<string>& words) {
            vector<string> results;
            vector<string> temp = {"qwertyuiop", "asdfghjkl", "zxcvbnm"};
            int flag = 0;
            int i, j, x;
            for(i = 0; i < words.size(); i++)
            {
                flag = 0;
                for(j = 0; j < words[i].size(); j++)
                {
                    for(x = 0; x < temp.size(); x++)
                    {
                        if(temp[x].find(tolower(words[i][j])) != string::npos)
                        {
                            if(x==0)
                                flag |= 1;
                            else if(x==1)
                                flag |= 2;
                            else
                                flag |= 4;
                        }
                    }
                    if(numOfBit(flag)>1)
                        break;
                }
                if(j>=words[i].size())
                    results.push_back(words[i]);
            }
            return results;
        }
    };
    

Log in to reply
 

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