题目:

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.

代码:

class Solution {
public:
vector<string> findWords(vector<string>& words) {
map<char, int> m;
vector<char> s1 = {'q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p'};
vector<char> s2 = {'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l'};
vector<char> s3 = {'z', 'x', 'c', 'v', 'b', 'n', 'm'};
for (auto c : s1)
m.insert(pair<char, int> (c, ));
for (auto c : s2)
m.insert(pair<char, int> (c, ));
for (auto c : s3)
m.insert(pair<char, int> (c, )); vector<string> result;
for (auto w : words){
bool b = ; #判断是否在一行
char first = tolower(w[]);
int i = m[first];
for (auto c : w ){
c = tolower(c);
int j = m[c];
if (j != i){
b = ;
break;
}
}
if (b)
result.push_back(w);
}
return result;
}
};

别人的:

 class Solution {
public:
vector<string> findWords(vector<string>& words) { vector<string> ans;
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(row1.count(c)) one = ;
if(row2.count(c)) two = ;
if(row3.count(c)) three = ;
if(one+two+three > ) break;
} if(one + two + three == ) ans.push_back(word);
} return ans; }
};
 ASCII编码:http://www.doc88.com/p-951217962470.html

LeetCode: 500 Keyboard Row (easy)的更多相关文章

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

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

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

  8. 【leetcode】500. Keyboard Row

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

  9. 500. Keyboard Row

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

随机推荐

  1. Ubuntu引导出问题grub rescu模式下:“error : unknown filesystem”或者 找不到normal.mod 的解决办法

    感谢http://www.linuxidc.com/Linux/2012-06/61983.htm,因为参考了其中的内容. 下面是修改和完善. 问题原因: (win7,ubuntu双系统下) 强制关机 ...

  2. 对canvas arc()中counterclockwise参数的一些误解

    一直没有很细心地去研究CanvasRenderingContext2D对象的arc方法,对它的认识比较模糊,导致犯了一些错误,特发此文,以纠正之前的错误理解. arc()方法定义如下: arc() 方 ...

  3. Oracle SQL_杂记

    1. 查询当前用户角色,当前用户下的表权限以及所有用户表权限 desc user_role_privs;select * from user_role_privs; desc user_tab_pri ...

  4. openwrt spi flash 分区适配过程

    openwrt spi flash 分区适配过程 这里基于 openwrt mt7620a 平台来跟踪,主要是想理清 dts 里的分区描述是如何一步步转化成内核分区行为. 先来看看 dts 中关于分区 ...

  5. Appium python自动化测试系列之Capability介绍(五)

    ​5.1 Capability介绍 5.1.1 什么是Capability 在讲capability之前大家是否还记得在讲log时给大家看过的启动时的日志?在我们的整个启动日志中会出现一些配置信息,其 ...

  6. MonoTouch.Dialog简介

    新建一个Single View Application项目 添加程序集 MonoTouch.Dialog.dll引用 删除 MainStoryboard.storyboard 添加空类Task.cs ...

  7. 九度OJ 1087:约数的个数 (数字特性)

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:7349 解决:2306 题目描述: 输入n个整数,依次输出每个数的约数的个数 输入: 输入的第一行为N,即数组的个数(N<=1000) ...

  8. Android笔记之dp与px之间的转换以及LayoutParams

    dp与px之间的转换公式 px = dp * (dpi / 160) dp = px / (dpi / 160) 其中dpi的获取方式如下 private void getDpi() { Displa ...

  9. python数据分析之ipython

    在用python进行数据分析的时候,需要提前安装如下几个库: Numpy:是python进行科学计算的科学包 pandas:提供了能够快速便捷地处理结构化数据的大量数据结构和函数 matplotlib ...

  10. 1.JavaScript:写入 HTML 输出

    ①JavaScript 是可插入HTML页面的编程代码 ②JavaScript插入HTML页面后,可有所有的现代浏览器执行 ※提示:您只能在 HTML 输出中使用 document.write.如果您 ...