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. window 计算机 开启事务

    window 操作系统如何开启事务 c#开发中使用事务调试程序的时候必须开启本地计算机的事务,如何开启呢: 1:控制面板 2:组件服务 3:本地DTC 4:设置 5:应用成功.

  2. 基于kubernetes集群的Vitess最佳实践

    概要 本文主要说明基于kubernetes集群部署并使用Vitess; 本文假定用户已经具备了kubernetes集群使用环境,如果不具备请先参阅基于minikube的kubernetes集群搭建, ...

  3. MQ中间件对比

  4. Career Planning:Developers Best Practices Tutorial

    This small tutorial is based on my past 16+ years of experience in software development industry. I ...

  5. Android Studio设置行宽、格式化断行

    设置基于Android studio 1.2,其它版本可能位置不大一样,可以直接搜索 1.设置行宽 就是那条右标准线的位置:Setting-->Editor-->Code Style,右侧 ...

  6. 转:探索C++0x: 1. 静态断言(static_assert)

    转自:http://www.cppblog.com/thesys/articles/116985.html 简介 C++0x中引入了static_assert这个关键字,用来做编译期间的断言,因此叫做 ...

  7. NHibernate应用开发

    第一章:NHibernate入门       第一讲:NHibernate架构剖析       第二讲:搭建第一个NHibernate应用程序       第三讲:nhibernate.cfg.xml ...

  8. oracle中记录被另一个用户锁住的原因与解决办法

    oracle数据中删除数据时提示“记录被另一个用户锁住” 解决方法: 1.查看数据库锁,诊断锁的来源及类型: select object_id,session_id,locked_mode from ...

  9. HDU - 6386 Age of Moyu 2018 Multi-University Training Contest 7 (Dijkstra变型)

    题意:N个点M条边的无向图,每条边都有属于自己的编号,如果一条路径上的边编号都相同,那么花费仅为1:改变至不同编号的路径,花费加1,无论这个编号之前是否走过. 分析:记录每个点的最小花费,再用set维 ...

  10. hadoop23---自定义rpc架构(duboo的原理)