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:

  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.

这道题给了我们一些单词,问哪些单词可以由键盘上的一行中的键符打出来,难度其实并不大。首先我们把键盘的三行字符分别保存到三个set中,然后遍历每个单词中的每个字符,分别看当前字符是否在三个集合中,如果在,对应的标识变量变为1,我们统计三个标识变量之和就知道有几个集合参与其中了,参见代码如下:

class Solution {
public:
vector<string> findWords(vector<string>& words) {
vector<string> res;
unordered_set<char> row1{'q','w','e','r','t','y','u','i','o','p'};
unordered_set<char> row2{'a','s','d','f','g','h','j','k','l'};
unordered_set<char> row3{'z','x','c','v','b','n','m'};
for (string word : words) {
int one = , two = , three = ;
for (char c : word) {
if (c < 'a') c += ;
if (row1.count(c)) one = ;
if (row2.count(c)) two = ;
if (row3.count(c)) three = ;
if (one + two + three > ) break;
}
if (one + two + three == ) res.push_back(word);
}
return res;
}
};

参考资料:

https://discuss.leetcode.com/topic/77754/java-1-line-solution-via-regex-and-stream

LeetCode All in One 题目讲解汇总(持续更新中...)

[LeetCode] Keyboard Row 键盘行的更多相关文章

  1. [LeetCode] 500. Keyboard Row 键盘行

    Given a List of words, return the words that can be typed using letters of alphabet on only one row' ...

  2. 500 Keyboard Row 键盘行

    给定一个单词列表,只返回可以使用在键盘同一行的字母打印出来的单词. 详见:https://leetcode.com/problems/keyboard-row/description/ C++: cl ...

  3. Leetcode500.Keyboard Row键盘行

    给定一个单词列表,只返回可以使用在键盘同一行的字母打印出来的单词.键盘如下图所示. 示例: 输入: ["Hello", "Alaska", "Dad& ...

  4. LeetCode——Keyboard Row

    LeetCode--Keyboard Row Question Given a List of words, return the words that can be typed using lett ...

  5. leetcode No.500 键盘行 keyboard-row (Python3实现)

    题目描述 给定一个单词列表,只返回可以使用在键盘同一行的字母打印出来的单词.键盘如下图所示. 示例: 输入: ["Hello", "Alaska", " ...

  6. 力扣(LeetCode)500. 键盘行

    给定一个单词列表,只返回可以使用在键盘同一行的字母打印出来的单词.键盘如下图所示. 示例: 输入: ["Hello", "Alaska", "Dad& ...

  7. LeetCode: Keyboard Row

    代码长了些,但还是比较简单的 public class Solution { public String[] findWords(String[] words) { List<String> ...

  8. Leetcode#500. Keyboard Row(键盘行)

    题目描述 给定一个单词列表,只返回可以使用在键盘同一行的字母打印出来的单词.键盘如下图所示. 示例1: 输入: ["Hello", "Alaska", &quo ...

  9. [LeetCode] 651. 4 Keys Keyboard 四键的键盘

    Imagine you have a special keyboard with the following keys: Key 1: (A): Print one 'A' on screen. Ke ...

随机推荐

  1. python socket编程制作后门木马(原创)

    不知道为啥,懒得解释怎么写出来的,直接上代码,爱看不看 Client:  简单连接服务器 #!/usr/bin/python #coding:utf-8 import socket import sy ...

  2. python 函数基础 定义

    一.函数介绍 1.为什么要有函数? 没有函数的代码组织结构不清晰,可读性差. 代码冗余 管理维护难度大,扩展性 2.什么是函数? 具备某一个功能的工具就是程序中的函数. 事先准备工具的过程就是:函数的 ...

  3. 如何在win10查看wifi密码

    tep1 找到wifi图标 step 2 右键点击打开网络共享中心 没有啦!!

  4. 十分钟释疑Oracle中“小表超慢”之谜(SQL调优/SQL优化)

    前几天,一个用户找到我,说查一个小表的时候非常慢,我问有多慢,他说最快也得半个小时才能出结果,有时干脆不出结果,我说小表多大,他说就几十兆,有点疑惑,让他帮忙获取了相关信息,一看就明白了,原来所谓的小 ...

  5. 和sin有关的代码

    include include using namespace std; const double TINY_VALUE=1e-10; double tsin(double x) { double g ...

  6. Leetcode 17.——Letter Combinations of a Phone Number

    Given a digit string, return all possible letter combinations that the number could represent. A map ...

  7. Java连接mysql——Establishing SSL connection without server's identity verification is not recommended.

    Establishing SSL connection without server's identity verification is not recommended. 出现这个错误的原因是因为m ...

  8. 2018上C语言程序设计(高级)- 第0次作业成绩

    作业链接: https://edu.cnblogs.com/campus/hljkj/CS201702/homework/1617 评分规则 本次作业作为本学期的第一次作业,大家态度较诚恳,篇幅都比较 ...

  9. C语言的第一次作业

    一.PTA实验作业 题目1. 温度转换 本题要求编写程序,计算华氏温度150°F对应的摄氏温度.计算公式:C=5×(F−32)/9,式中:C表示摄氏温度,F表示华氏温度,输出数据要求为整型. 1.实验 ...

  10. alpha-咸鱼冲刺day2-紫仪

    总汇链接 一,合照 emmmmm.自然是没有的. 二,项目燃尽图 三,项目进展 今天并没有什么进展,弄了好久好像也只研究出怎么把JS的功能块插入进去.html的信息提交这些还不知道要怎么弄. 四,问题 ...