LeetCode500. Keyboard Row我的C++解法

来源:互联网 发布:加工中心倒角c怎么编程 编辑:程序博客网 时间:2024/05/22 03:53

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


American keyboard


Example 1:

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

Note:

  1. You may use one character in the keyboard more than once.

  1. You may assume the input string will only contain letters of alphabet.
class Solution {public:    vector<string> findWords(vector<string>& words) {        string rows[] = {"qwertyuiop", "asdfghjkl", "zxcvbnm"};        vector<string> vs;        for (int i = 0; i < words.size(); i++)        {            string s = words[i];            int r = -1;            int j = 0;            for (j = 0; j < s.size(); j++) {                if (r == -1) {                    while (r < 2) {                        r++;                        if (string::npos != rows[r].find(tolower(s[j]))) {                            break;                        }                    }                } else {                    if (string::npos == rows[r].find(tolower(s[j]))) {                           break;                    }                }            }            if (j == s.size()) {                vs.push_back(s);            }        }        return vs;    }};


0 0
原创粉丝点击