problem

500. Keyboard Row

题意:判断给出的某个单词是否可以使用键盘上的某一行字母type得到;

注意大小写的转换;

solution1: 使用set保存三行字符,查看每个字符所在的行数是否一致;

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(int i=; i<words.size(); i++)
{
int one = , two =, three = ;
for(auto ch : words[i])
{
if(ch<'a') ch += ;//
if(row1.count(ch)) one = ;
if(row2.count(ch)) two = ;
if(row3.count(ch)) three = ;
if(one+two+three>) break;
}
if(one+two+three==) ans.push_back(words[i]);
}
return ans;
}
};

SET:

set containers are generally slower than unordered_set containers to access individual elements by their key, but they allow the direct iteration on subsets based on their order.

or:

判断每一行的字母数目是否与对应单词的长度一致;

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(int i=; i<words.size(); i++)
{
int one = , two =, three = ;
for(auto ch : words[i])
{
if(ch<'a') ch += ;//
if(row1.count(ch)) one++;
if(row2.count(ch)) two++;
if(row3.count(ch)) three++;
}
int num = words[i].size();
if(one==num || two==num || three==num) ans.push_back(words[i]);
}
return ans;
}
};

solution2: 使用map映射键盘上每个字母所在的行数,判断某个单词每个字母所在行数是否一致;

参考

1. Leetcode_500. Keyboard Row;

2. GrandYang

【leetcode】500_Keyboard Row的更多相关文章

  1. 【LeetCode】623. Add One Row to Tree 解题报告(Python)

    [LeetCode]623. Add One Row to Tree 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problem ...

  2. 【Leetcode】Pascal&#39;s Triangle II

    Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3 ...

  3. 【LeetCode】哈希表 hash_table(共88题)

    [1]Two Sum (2018年11月9日,k-sum专题,算法群衍生题) 给了一个数组 nums, 和一个 target 数字,要求返回一个下标的 pair, 使得这两个元素相加等于 target ...

  4. 【leetcode】688. Knight Probability in Chessboard

    题目如下: On an NxN chessboard, a knight starts at the r-th row and c-th column and attempts to make exa ...

  5. 【LeetCode】Pascal's Triangle II 解题报告

    [LeetCode]Pascal's Triangle II 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/pascals-tr ...

  6. 【LeetCode】120. Triangle 解题报告(Python)

    [LeetCode]120. Triangle 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址htt ...

  7. 【LeetCode】779. K-th Symbol in Grammar 解题报告(Python)

    [LeetCode]779. K-th Symbol in Grammar 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingz ...

  8. 【LeetCode】36. Valid Sudoku 解题报告(Python)

    [LeetCode]36. Valid Sudoku 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址 ...

  9. 【LeetCode】813. Largest Sum of Averages 解题报告(Python)

    [LeetCode]813. Largest Sum of Averages 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博 ...

随机推荐

  1. hive动态分区常用参数

    set mapreduce.job.queuename=root.sc;set hive.exec.dynamic.partition=true;set hive.exec.dynamic.parti ...

  2. Python3+Appium学习笔记05-报错及解决方法

    记录一下使用期间各种报错和解决方法,毕竟搜了半天才找到解决方法.另外提示一下.优先看官方文档. 报错前面都是一样,就是说在处理的时候,服务器发生了一个未知的错误.然后才是具体报错信息 1)seleni ...

  3. #Python绘制 文本进度条,带刷新、时间暂缓的

    #Python绘制 文本进度条,带刷新.时间暂缓的 #文本进度条 import time as T st=T.perf_counter() print('-'*6,'执行开始','-'*6) maxx ...

  4. Procomm Plus 与ASPECT脚本语言在基于远程终端设备上的测试应用

    产测 ---------------------------------------------------- 原文:http://www.bixuanzl.com/20180801/1084478. ...

  5. 转发:i p _ f o r w a r d函数

    转发:i p _ f o r w a r d函数到达非最终目的地系统的分组需要被转发.只有当 i p f o r w a r d i n g非零或当分组中包含源路由时,i p i n t r才调用实现 ...

  6. 05_centos7安装python3

    https://www.cnblogs.com/FZfangzheng/p/7588944.html https://www.cnblogs.com/simuhunluo/p/7704765.html ...

  7. E:last-child

    E:last-child 语法: E:last-child { sRules } 说明: 匹配父元素的最后一个子元素E.大理石平台厂家大理石平台厂家 要使该属性生效,E元素必须是某个元素的子元素,E的 ...

  8. [Luogu] 矩阵加速(数列)

    题面:https://www.luogu.org/problemnew/show/P1939 题解:https://www.zybuluo.com/wsndy-xx/note/1153810

  9. char 类型的数组转换到CSting

    首先,字符串“abc”在CString的保存格式是‘a’,'\0','b','\0','c','\0','\0','\0'; 从中可以看出它是以‘\0’,'\0',结束的. 当char ch[6]: ...

  10. 2019牛客暑期多校训练营(第六场)E 构造、原图是补图的同构图

    https://ac.nowcoder.com/acm/contest/886#question 题意  问是否存在某个n个点的无向图G是其补图H的同构图,若存在输出G的邻接矩阵以及H关于G的映射. ...