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. GAN学习指南:从原理入门到制作生成Demo,总共分几步?

    来源:https://www.leiphone.com/news/201701/yZvIqK8VbxoYejLl.html?viewType=weixin 导语:本文介绍下GAN和DCGAN的原理,以 ...

  2. C#readonly和const对比

    readonly:常量,只能在声明时或构造函数中赋值,与static连用表示静态常量 const:静态常量,只能在声明时赋值

  3. MATLAB中.m文件生成.dll

    1.配置编译环境 在命令行窗口输入: mbuild -setup 如果出现以下提示信息说明成功: 如果提示信息为: 错误使用mbuild(line 164) Unable to complete su ...

  4. python----获取文件的路径(各种方式)

    #获取到当前文件的上个文件夹目录ABSPATH = os.path.abspath(os.path.realpath(os.path.dirname(__file__))) #获取的是相对路径FRON ...

  5. [译] 尤雨溪:Vue 3.0 计划

    [译] 尤雨溪:Vue 3.0 计划 原文:Plans for the Next Iteration of Vue.js作者:Evan You 发表时间:Sep 30, 2018译者:西楼听雨 发表时 ...

  6. 2019HDU多校第六场 6641 TDL——乱搞&&思维题

    题意 设 $f(n, m)$ 为大于 $n$ 且与 $n$ 互质的数中第 $m$ 小的数,求满足 $(f(n, m) - n) \oplus n = k$ 的最小正整数 $n$ 分析 因为 $m \l ...

  7. C# MVC中直接执行Js

    .NET MVC里如何在服务器端执行JS: 三种解决方案: 1.直接返回JavaScript. public ActionResult XXXAction1() {      return JavaS ...

  8. bootstrap的css和js

    css:<link href="http://cdn.bootcss.com/bootstrap/3.3.6/css/bootstrap.min.css" rel=" ...

  9. maven+struts2例子

    本文在开发第一个maven示例的基础上进行扩展. 第一个maven示例目录结构如下: 扩展后目录为: 打开pom.xml 在WEB-INF文件夹下新建web.xml: <?xml version ...

  10. 基于steam的游戏销量预测 — PART 3 — 基于BP神经网络的机器学习与预测

    语言:c++ 环境:windows 训练内容:根据从steam中爬取的数据经过文本分析制作的向量以及标签 使用相关:无 解释: 就是一个BP神经网络,借鉴参考了一些博客的解释和代码,具体哪些忘了,给出 ...