LeetCode——Keyboard Row

Question

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.

American keyboard

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.

Answer


class Solution {
public:
vector<string> findWords(vector<string>& words) {
string str1 = "qwertyuiop";
string str2 = "asdfghjkl";
string str3 = "zxcvbnm"; vector<string> res;
for (string str : words) {
char c = str[0] >= 97 ? str[0] : str[0] + 32;
if (str1.find(c) != string::npos) {
if (judge(str, str1))
res.push_back(str);
} else if (str2.find(c) != string:: npos) {
if (judge(str, str2))
res.push_back(str);
} else {
if (judge(str, str3))
res.push_back(str);
}
}
return res;
} int judge(string str, string str1) {
int flag = 1;
for (int i = 1; i < str.length(); i++) {
char c = str[i] >= 97 ? str[i] : str[i] + 32;
if (str1.find(c) == string::npos) {
flag = 0;
break;
}
}
return flag;
}
};

LeetCode——Keyboard Row的更多相关文章

  1. [LeetCode] Keyboard Row 键盘行

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

  2. LeetCode: Keyboard Row

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

  3. 46. leetcode 500. Keyboard Row

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

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

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

  5. 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 ...

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

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

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

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

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

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

  9. leetcode算法: Keyboard Row

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

随机推荐

  1. Adapter适配器 final int Id 导致选中的Item不在当前界面

    写了上面这么一个横向混动,点击切换到,哪个的Item上就会有一个  常用  的小图标.但是我每次滑动切换到后面   成龙9这个Item,这个 常用的图片,也在 这个上面了,但是他一更新,就变成 等你再 ...

  2. JStorm开发经验+运维经验总结

    1.开发经验总结  ——12 Sep 2014 · 8 revisions 在jstorm中, spout中nextTuple和ack/fail运行在不同的线程中, 从而鼓励用户在nextTuple里 ...

  3. 【BZOJ1922】[Sdoi2010]大陆争霸 Dijkstra

    Description 具体地说,杰森国有 N 个城市,由 M条单向道 路连接.神谕镇是城市 1而杰森国的首都是城市 N.你只需摧毁位于杰森国首都 的曾·布拉泽大神殿,杰森国的信仰,军队还有一切就都会 ...

  4. java的奇技淫巧--意外行为与特性(译文)

    Java是一种非常成熟的编程语言 - 事实上,它已经走过21年了,如果它是一个人,它可以在美国随便混!随着年龄的增长,智慧也在增长,而至少有时候,有些东西会变得很怪异.在本文中,我将介绍Java语言的 ...

  5. css 中的事件冒泡

    css伪类中的表现类似于事件冒泡的,举个例子,当你滑过一个元素时,他会认为你也滑过了该元素的父元素,即使该元素看起来并没有包含在父元素里面,此处以:hover例子: 效果图: 滑过前: 滑过后: CS ...

  6. 关于canvas绘制大转盘并旋转

    O(∩_∩)O包子不才,最近磕磕巴巴写了一个大转盘的效果.现在想说一下整个的思路部分,要是有设么不对的还请多多指教,期待共同成为优秀的前端~~大转盘整个思路: 绘制整个转盘 编写一个随机数,用来当接口 ...

  7. Map,Filter和Reduce

    转自:https://www.aliyun.com/jiaocheng/444967.html?spm=5176.100033.1.13.xms8KG 摘要:Map,Filter和Reduce三个函数 ...

  8. Windows服务的调试

    1.服务为其他程序调用的情况:首先停止服务,在项目中设置断点,重新启动服务,点击项目中工具,附加到进程,运行调用服务的程序,即可进入之前设置的断点,进而进行调试. 2.服务内方法为自动执行的情况:首先 ...

  9. jq左右按钮点击幻灯片

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/ ...

  10. 转!!Tomcat网站上的core和deployer的区别

    转自:https://www.cnblogs.com/guxia/p/6678184.html 8.5.13 Please see the README file for packaging info ...