500. Keyboard Row

Problem:

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.

Example:

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.

思路

Solution (C++):

vector<string> findWords(vector<string>& words) {
    vector<int> dict(26, 0);
    vector<string> rows{"qwertyuiop", "asdfghjkl", "zxcvbnm"};
    vector<string> res;
    
    for (int i = 0; i < rows.size(); ++i) {
        for (auto c : rows[i]) {
            dict[c-'a'] = 1 << i;
        }
    }
    
    for (auto w : words) {
        int base = 7;
        for (auto c : w) {
            base &= dict[tolower(c)-'a'];
            if (base == 0)  break;
        }
        if (base)  res.push_back(w);
    }
    return res;
}

性能

Runtime: 0 ms  Memory Usage: 6.2 MB

思路

Solution (C++):


性能

Runtime: ms  Memory Usage: MB

相关链接如下:

知乎:littledy

欢迎关注个人微信公众号:小邓杂谈,扫描下方二维码即可

作者:littledy
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文链接,否则保留追究法律责任的权利。
原文地址:https://www.cnblogs.com/dysjtu1995/p/12692456.html