【leetcode】500_Keyboard Row
problem
题意:判断给出的某个单词是否可以使用键盘上的某一行字母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的更多相关文章
- 【LeetCode】623. Add One Row to Tree 解题报告(Python)
[LeetCode]623. Add One Row to Tree 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problem ...
- 【Leetcode】Pascal's Triangle II
Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3 ...
- 【LeetCode】哈希表 hash_table(共88题)
[1]Two Sum (2018年11月9日,k-sum专题,算法群衍生题) 给了一个数组 nums, 和一个 target 数字,要求返回一个下标的 pair, 使得这两个元素相加等于 target ...
- 【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 ...
- 【LeetCode】Pascal's Triangle II 解题报告
[LeetCode]Pascal's Triangle II 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/pascals-tr ...
- 【LeetCode】120. Triangle 解题报告(Python)
[LeetCode]120. Triangle 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址htt ...
- 【LeetCode】779. K-th Symbol in Grammar 解题报告(Python)
[LeetCode]779. K-th Symbol in Grammar 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingz ...
- 【LeetCode】36. Valid Sudoku 解题报告(Python)
[LeetCode]36. Valid Sudoku 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址 ...
- 【LeetCode】813. Largest Sum of Averages 解题报告(Python)
[LeetCode]813. Largest Sum of Averages 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博 ...
随机推荐
- 大数据之路week04--day05(java XML解析)
java解析XML的四种方式: XML是一种通用的数据交换格式,它的平台无关性.语言无关性.系统无关性.给数据集成与交互带来了极大的方便.XML在不同的语言环境中解析方式都是一样的,只不过实现的语法不 ...
- 0004SpringBoot整合Redis
在已经整合了SpringDataJPA和Junit的基础上,整合Redis,只需要一下几步即可: 1.下载64windows版的Redis安装包.解压并启动服务端 2.配置Redis的起步依赖(pom ...
- XML 命名规范
XML 元素必须遵循以下命名规则: 名称可以含字母.数字以及其他的字符 名称不能以数字或者标点符号开始 名称不能以字符 "xml"(或者 XML.Xml)开始 名称不能包含空格 可 ...
- BZOJ 1778: [Usaco2010 Hol]Dotp 驱逐猪猡 (高斯消元)
题面 题目传送门 分析 令爆炸概率为PPP.设 f(i)=∑k=0∞pk(i)\large f(i)=\sum_{k=0}^{\infty}p_k(i)f(i)=∑k=0∞pk(i),pk(i)p ...
- 23、自动装配-Aware注入Spring底层组件&原理
23.自动装配-Aware注入Spring底层组件&原理 Aware 接口,提供了类似回调函数的功能 自定义组件想要使用Spring 容器底层的一些组件(Application Context ...
- pat 甲级 1057 Stack(30) (树状数组+二分)
1057 Stack (30 分) Stack is one of the most fundamental data structures, which is based on the princi ...
- sql server解锁表
EXEC sp_who active --看哪个引起的阻塞,blk blk<>0 --解锁表declare @spid intSet @spid = 274 --锁表进程declare @ ...
- Editor
E. Editor 我们把"("用1表示,")"用-1表示,其余字母用0表示,这样形成的一个数组,我们求出它的前缀和sum[],只有当\(sum[n]==0\) ...
- POJ 1661 Help Jimmy ——(记忆化搜索)
典型的记忆化搜索问题,dfs一遍即可.但是不知道WA在哪里了= =,一直都没找出错误.因为思路是很简单的,肯定是哪里写挫了,因此不再继续追究了. WA的代码如下,希望日后有一天能找出错误= =: —— ...
- java学习之路(1)
java中的main()方法: 一.java主类中的主方法形式: public static void main(String[] args){ //TODO } 二.个部分解释: (1).publi ...