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. 导弹拦截( 二分+dilworth定理)

    https://www.luogu.org/problemnew/show/P1020 原题 接下来是dilworth定理 https://blog.csdn.net/u011676717/artic ...

  2. Pandas-Numpy-Scipy-Matplotlib

    Ref:Using iloc, loc, & ix to select rows and columns in Pandas DataFrames 单纯的方括号只有两个用途:基于列的label ...

  3. 企业IT运维以及信息管理部服务器管理

    方法 1.服务器有必要保持简洁.除了必要的应用软件以及安全软件之外,尽量不要安全其它的软件. 2.要做好服务器帐号权利规划和分配,分配够用的权利就行,从而降低密码泄漏带来的损失. 3.注意关注服务器软 ...

  4. 2.webpack最基本的使用方式

    什么是webpack? webpack是前端的一个项目构建工具,它是基于Node.js开发出来的一个前端工具: webpack安装的两种方式 1.运行 'npm i webpack -g' 全局安装w ...

  5. 大二小学期C#资产管理大作业小记

    说明 这个程序是我大二夏季学期(俗称小学期)用Visual Studio + C#写的<资产管理>大作业.这个项目非常简单,就是用C#写出一个UI界面,并连接数据库进行增删改查.这是我第一 ...

  6. Python 10.1.1

  7. Educational Codeforces Round 74 (Rated for Div. 2)【A,B,C【贪心】,D【正难则反的思想】】

    A. Prime Subtractiontime limit per test2 secondsmemory limit per test256 megabytesinputstandard inpu ...

  8. Asteroids POJ - 3041 【最小点覆盖集】

    Bessie wants to navigate her spaceship through a dangerous asteroid field in the shape of an N x N g ...

  9. git error:1407742E:SSL routines:SSL23_GET_SERVER_HELLO:tlsv1 alert protocol version

    原因:git 版本过低 解决方法:卸载旧版本,重装新版本

  10. 【概率论】4-4:距(Moments)

    title: [概率论]4-4:距(Moments) categories: - Mathematic - Probability keywords: - Moments - Moments Gene ...