【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 个人博 ...
随机推荐
- nuxt入门
之前一直都是做vue-spa单页面,不利于SEO.而便于SEO的SSR(服务器端渲染)多页应用,可以使用nuxt.js这个框架来实现 (0)nuxt安装 npx create-nuxt-app < ...
- gdb设置条件断点
b +行号 if i==9:设置条件断点 finish:执行到当前函数返回处(退出函数) bt:打印栈帧关系
- js 同步 异步 宏任务 微任务 文章分享
分享一篇 写的很好的 宏任务 微任务 同步异步的文章 文章原地址: https://juejin.im/post/59e85eebf265da430d571f89 这一次,彻底弄懂 JavaScri ...
- springboot mybatis 的SQL异常不输出错误到控制台问题排查
项目中使用springboot集成 mybatis,运行过程中查询SQL列在表中不存在,但系统不输出任何错误到控制台 但SQL是打印的,只是没有任何异常信息 将SQL复制出来到数据库中运行,才发现错误 ...
- CallContext线程数据缓存-调用上下文
一.CallContext 概述 命名空间:System.Runtime.Remoting.Messaging CallContext 用于提供与执行代码路径一起传送的属性集,直白讲就是:提供线程(多 ...
- node.js中允许的app对象声明方式
伪对象形式 app = function () { console.log("我是一个初始化的app对象"); }; app.get=function () { console.l ...
- php注意事项|要点
1. 用单引号代替双引号来包含字符串,这样做会更快一些.因为 PHP 会在双引号包围的 字符串中搜寻变量,单引号则不会,注意:只有 echo 能这么做,它是一种可以把多个字符 串当作参数的“函数”(译 ...
- 使用 ALinq 实现 Linq to MySQL【转】
http://www.cnblogs.com/huangcong/archive/2011/05/24/2055204.html
- CSPS模拟69-72
模拟69: T1,稍数学,主要还是dp(转移莫名像背包???),当C开到n2时复杂度为n4,考场上想了半天优化结果发现n是100,n4可过 #include<iostream> #incl ...
- gitlab高可用模式
高可用模式 企业版 社区版 我们这里说一下成本比较低的主备模式,它主要依赖的是DRBD方式进行数据同步,需要2台ALL IN ONE的GitLab服务器,也就是通过上面安装方式把所有组件都安装在一起的 ...