Leetcode刷题(2)

来源:互联网 发布:netbsd源码 编辑:程序博客网 时间:2024/05/20 17:27

Given a List of words, returnthe words that can be typed using letters of alphabet ononly one row's of American keyboard like the image below.


Example 1:

Input: ["Hello", "Alaska", "Dad", "Peace"]
Output: ["Alaska", "Dad"]

Note:

  1. You may use one character in the keyboard more than once.
  2. You may assume the input string will only contain letters of alphabet.

Subscribe tosee which companies asked this question.

 

 

1. 首先先对vector<string> &words中的string进行遍历

2. 将string分解为单个字母

char *dst = new char[255];

    int i; 

    for(i=0;i <=src.length();i++) 

        dst[i]=src[i]; 

    dst[i] = '\0';

     return dst;

 

3. 循环,看这个string中的字母是否都在一行,是则输出,不是则不输出

!一定要注意大小写的转换

代码部分

classSolution {

public:

    vector<string>findWords(vector<string>& words) {

        char l1[10] ={'q','w','e','r','t','y','u','i','o','p'};

        char l2[9] ={'a','s','d','f','g','h','j','k','l'};

        char l3[7] = {'z','x','c','v','b','n','m'};

        vector<string> outputs;

        bool a=false,b=false,c=false;

        int m=0,n=0,o=0;

        for(int it=0;it<words.size();it++) {

        

            string str = words[it];

          

            for(int i=0; i<str.size(); i++){

                for(int k=0; k<10; k++) {

                    if((char)tolower(str[i]) ==l1[k])

                       ++m;

               }

            }

            if(m == str.size()) a = true;

           

         

           

            for(int i=0; i<str.size(); i++){

                for(int k=0; k<9; k++) {

                    if((char)tolower(str[i]) ==l2[k])

                       ++n;

                 

                }

            }

            if(n == str.size()) b = true;

          

           

            for(int i=0; i<str.size(); i++){

                for(int k=0; k<7; k++) {

                    if((char)tolower(str[i]) ==l3[k])

                       ++o;

                      

                }

            }

            if(o == str.size()) c = true;

          

            if(a || b || c)

              {outputs.push_back(str);}

             

            m=n=o=0;

            a=b=c=false;

        }

        return outputs;

    }

};

 

 

参考代码

classSolution {

public:

    vector<string>findWords(vector<string>& words) {

        unordered_set<char> row1 {'q','w', 'e', 'r', 't', 'y','u', 'i', 'o', 'p'};

        unordered_set<char> row2 {'a','s', 'd', 'f', 'g', 'h', 'j', 'k', 'l'};

        unordered_set<char> row3 { 'z','x', 'c', 'v', 'b' ,'n', 'm'};

        vector<unordered_set<char>>rows {row1, row2, row3};

       

       

        vector<string> validWords;

        for(int i=0; i<words.size(); ++i){

            int row=0;

           

            for(int k=0; k<3; ++k){

               if(rows[k].count((char)tolower(words[i][0])) > 0) row = k;

            }

           

            validWords.push_back(words[i]);

            for(int j=1; j<words[i].size();++j){

               if(rows[row].count((char)tolower(words[i][j])) == 0){

                    validWords.pop_back();

                    break;

                }

            }

           

        }

        return validWords;

    }

};

 

总结:参考代码是先将函数定位到某一行,然后进行性判别,如果有某个字母不是在这一行,就移除这个元素。

0 0
原创粉丝点击