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

原文链接: https://www.cnblogs.com/dysjtu1995/p/12692456.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍;

也有高质量的技术群,里面有嵌入式、搜广推等BAT大佬

    500. Keyboard Row

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/341895

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年3月2日 上午1:18
下一篇 2023年3月2日 上午1:19

相关推荐