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. hdu3625(第一类斯特林数)

    与第二类有些区别! #include <stdio.h> #include <stdlib.h> #include <string.h> #include < ...

  2. Leetcode-Test Justification

    Given an array of words and a length L, format the text such that each line has exactly L characters ...

  3. [LintCode] 通配符查询

    动态规划: class Solution { public: /** * @param s: A string * @param p: A string includes "?" ...

  4. 手动爬虫之京东笔记本栏(ptyhon3)

    import urllib.request as ur import urllib.error as ue import re # 目标网址 url = 'https://list.jd.com/li ...

  5. linux系统各种日志存储路径和详细介绍

    Linux常见的日志文件详述如下1./var/log/boot.log(自检过程)2./var/log/cron (crontab守护进程crond所派生的子进程的动作)3./var/log/mail ...

  6. python基础之类和对象、对象之间的交互、类名称空间与对象/实例名称空间

    一 面向对象初识 Python要么是面向过程要么是面向对象. 概念及优缺点: 面向过程的程序设计的核心是过程,过程即解决问题的步骤,面向过程的设计就好比精心设计好一条流水线,考虑周全什么时候处理什么东 ...

  7. PHP去除所有的空格

    1.去除两边的空格 trim($arr) 2.正则匹配去除所有的空格 preg_replace('# #','',$goodid)

  8. ZOJ 2770 Burn the Linked Camp 差分约束

    链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do? problemCode=2770 Burn the Linked Camp Time Limi ...

  9. python学习之路-第六天-一个简单的脚本

    现在有一个需求:把某个目录下的文件备份到指定到另外一个目录下,而且压缩后文件为zip文件 # -*- coding:utf-8 -*- #! /usr/bin/python # Filename:ba ...

  10. Hadoop源码如何查看

    如何查看hadoop源码 1解压hadoop安装压缩文件成为文件夹,再进入解压后的文件夹下的src文件夹,选中core,hdfs,mapred三个文件夹