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.

思路:

首先判断第一个字符是属于键盘上的哪一排,然后判断接下来的每一个字符是否都在这一排里出现。

 bool can(string str, vector<unordered_set<char>>& rows)
{
int row = ;
for (int k = ; k<; ++k)
{
if (rows[k].count((char)tolower(str[])) > ) row = k;
}
for (int i = ;i<str.size();i++)
{
if (rows[row].count((char)tolower(str[i])) == ) return false;
}
return true;
}
vector<string> findWords(vector<string>& words)
{
vector<string> ret;
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' }; vector<unordered_set<char>> rows{ row1, row2, row3 };
for(int i = ; i < words.size();i++)
{
if (can(words[i], rows))
{
ret.push_back(words[i]);
}
}
return ret;
}

参考:

https://discuss.leetcode.com/topic/77761/c-solution

[leetcode-500-Keyboard Row]的更多相关文章

  1. 46. leetcode 500. Keyboard Row

    500. Keyboard Row Given a List of words, return the words that can be typed using letters of alphabe ...

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

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

  3. LeetCode 500. Keyboard Row (键盘行)

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

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

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

  5. LeetCode 500 Keyboard Row 解题报告

    题目要求 Given a List of words, return the words that can be typed using letters of alphabet on only one ...

  6. LeetCode: 500 Keyboard Row (easy)

    题目: Given a List of words, return the words that can be typed using letters of alphabet on only one ...

  7. Week4 - 500.Keyboard Row & 557.Reverse Words in a String III

    500.Keyboard Row & 557.Reverse Words in a String III 500.Keyboard Row Given a List of words, ret ...

  8. 【LeetCode】500. Keyboard Row 解题报告(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 暴力解 字典 + set 日期 题目地址:https ...

  9. 【leetcode】500. Keyboard Row

    问题描述: Given a List of words, return the words that can be typed using letters of alphabet on only on ...

  10. 500. Keyboard Row

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

随机推荐

  1. jgs--多线程和synchronized

    多线程 多线程是我们开发人员经常提到的一个名词.为什么会有多线程的概念呢?我们的电脑有可能会有多个cpu(或者CPU有多个内核)这就产生了多个线程.对于单个CPU来说,由于CPU运算很快,我们在电脑上 ...

  2. 瀑布流原生ajax,demo

    最近听朋友们说起瀑布流挺多的,自己就去研究下了,一个简单的原生demo,分享给大家... 简单分为三个文档,有详细的注释 img:ajax.php:demo.php 其中img中放入图片 1.jpg: ...

  3. 初识数据库连接池开源框架Druid

    Druid是阿里巴巴的一个数据库连接池开源框架,准确来说它不仅仅包括数据库连接池这么简单,它还提供强大的监控和扩展功能.本文仅仅是在不采用Spring框架对Druid的窥探,采用目前最新版本druid ...

  4. bitnami gitlab 配置域名

    正常安装完成以后gitlab的代码仓库域名的地址依然是IP,这样不便于我们记忆,所以我想给gitlab增加一个域名 找到gitlab.yml 配置文件,在gitlab 节点下的host 由IP变更为域 ...

  5. Day2-字符编码转换

    1.在python2默认编码是ASCII, python3里默认是unicode 2.unicode 分为 utf-32(占4个字节),utf-16(占两个字节),utf-8(占1-4个字节), so ...

  6. carryLess开发日记_2017-05-18

    1.接上一篇的form表单的ajax问题,上一篇中的form表单的ajax提交不能上传文件,所以采用了formData的方式上传 1)前段代码如下: <form action="&qu ...

  7. Android利用文本分割拼接开发一个花藤文字生成

    今天研究了一个小软件,挺有意思的,尽管网上已经很多那种软件,但是今天还是在这里给大家分享一下这个软件的具体开发过程 首先,这个软件只需要三个主要控件,EditText.Button以及TextView ...

  8. 浅谈MVC异常处理

    在日常开发中,我们会去捕捉很多的异常,来进行处理,通常我们的方法就是,在需要进行异常处理的地方加上 try catch 块,但是,如果需要异常处理的地方很多,那么,就会频繁的去写try catch 块 ...

  9. Weighted Effect Coding: Dummy coding when size matters

    If your regression model contains a categorical predictor variable, you commonly test the significan ...

  10. cas4.2以下取消https

    deployerConfigContext.xml增加参数p:requireSecure="false" <bean class="org.jasig.cas.au ...