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;
    }
    };
    
  • 相关阅读:
    Java的HttpServletRequest
    Java的用户登录计数功能
    JAVA-DATE
    正则表达式
    Java-JDBC(2)
    Java-JDBC(1)
    String类
    java的多态和构造方法
    Java的封装 this关键字 继承
    java抽象类与接口
  • 原文地址:https://www.cnblogs.com/A-Little-Nut/p/10067703.html
Copyright © 2011-2022 走看看