zoukankan      html  css  js  c++  java
  • LeetCode 500. Keyboard Row

    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 1:

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

    Note:

    • You may use one character in the keyboard more than once.
    • You may assume the input string will only contain letters of alphabet.
    class Solution {
    public:
    vector<string> findWords(vector<string>& words) {
               map<char,int> checked={{'a',2},{'s',2},{'d',2},{'f',2},{'g',2},{'h',2},{'j',2},{'k',2},{'l',2},
                                      {'q',1},{'w',1},{'e',1},{'r',1},{'t',1},{'y',1},{'u',1},{'i',1},{'o',1},{'p',1},
                                      {'z',3},{'x',3},{'c',3},{'v',3},{'b',3},{'n',3},{'m',3}};
              vector<string> res;
              for(int i=0;i<words.size();i++){
              	  char ch=tolower(words[i][0]);
                  int line=checked[ch] , j ;
                  for(j=0;j<words[i].size();j++){
                         ch=tolower(words[i][j]);
                      if(checked[ch]!=line)
                         break;
                  }
                  if(j==words[i].size()) res.push_back(words[i]);
              }
              return res;
    }
    };
    
  • 相关阅读:
    一个网站架构的变迁
    网络编程
    http协议篇
    第1篇 编程能力是什么
    django中的cookies和session机制
    django的认证与授权系统
    python的异常处理
    第0篇
    mysql优化和全局管理杂记
    k8s中pod的资源配置详解
  • 原文地址:https://www.cnblogs.com/A-Little-Nut/p/10067703.html
Copyright © 2011-2022 走看看