【leetcode】500. Keyboard Row
问题描述:
Given a List of words, return the words that can be typed using letters of alphabet on only one row's of American keyboard like the image below.
Example 1:
- Input: ["Hello", "Alaska", "Dad", "Peace"]
- Output: ["Alaska", "Dad"]
Note:
- You may use one character in the keyboard more than once.
- You may assume the input string will only contain letters of alphabet.
解法一:
常规解法,用unordered_set存储每一行的字母,依次寻找判断。
- class Solution {
- public:
- vector<string> findWords(vector<string>& words) {
- 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'};
- vector<unordered_set<char>> rows {row1, row2, row3};
- vector<string> validWords;
- for(int i=; i<words.size(); ++i){
- int row=;
- for(int k=; k<; ++k){
- if(rows[k].count((char)tolower(words[i][])) > ) row = k;
- }
- validWords.push_back(words[i]);
- for(int j=; j<words[i].size(); ++j){
- if(rows[row].count((char)tolower(words[i][j])) == ){
- validWords.pop_back();
- break;
- }
- }
- }
- return validWords;
- }
- };
解法二:
这种解法比较有启发性,看起来很有意思。
- class Solution {
- public:
- vector<string> findWords(vector<string>& words)
- {
- vector<string> res;
- for(auto str : words)
- {
- bool r1 = str.find_first_of("QWERTYUIOPqwertyuiop") == string::npos ? false : true;
- bool r2 = str.find_first_of("ASDFGHJKLasdfghjkl") == string::npos ? false : true;
- bool r3 = str.find_first_of("ZXCVBNMzxcvbnm") == string::npos ? false : true;
- if(r1 + r2 + r3 == )
- res.push_back(str);
- }
- return res;
- }
- };
【leetcode】500. Keyboard Row的更多相关文章
- 【LeetCode】500. Keyboard Row 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 暴力解 字典 + set 日期 题目地址:https ...
- 【LeetCode】623. Add One Row to Tree 解题报告(Python)
[LeetCode]623. Add One Row to Tree 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problem ...
- 46. leetcode 500. Keyboard Row
500. Keyboard Row Given a List of words, return the words that can be typed using letters of alphabe ...
- Leetcode#500. Keyboard Row(键盘行)
题目描述 给定一个单词列表,只返回可以使用在键盘同一行的字母打印出来的单词.键盘如下图所示. 示例1: 输入: ["Hello", "Alaska", &quo ...
- 【leetcode】500_Keyboard Row
problem 500. Keyboard Row 题意:判断给出的某个单词是否可以使用键盘上的某一行字母type得到: 注意大小写的转换: solution1: 使用set保存三行字符,查看每个字符 ...
- 【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】 454、四数之和 II
题目等级:4Sum II(Medium) 题目描述: Given four lists A, B, C, D of integer values, compute how many tuples (i ...
- Week4 - 500.Keyboard Row & 557.Reverse Words in a String III
500.Keyboard Row & 557.Reverse Words in a String III 500.Keyboard Row Given a List of words, ret ...
- 【LeetCode】树(共94题)
[94]Binary Tree Inorder Traversal [95]Unique Binary Search Trees II (2018年11月14日,算法群) 给了一个 n,返回结点是 1 ...
随机推荐
- Android面试收集录15 Android Bitmap压缩策略
一.为什么Bitmap需要高效加载? 现在的高清大图,动辄就要好几M,而Android对单个应用所施加的内存限制,只有小几十M,如16M,这导致加载Bitmap的时候很容易出现内存溢出.如下异常信息, ...
- python面向对象的约束和自定义异常
基于人为来约束: 即人为主动抛出异常 class BaseMessage(object): def send(self,x1): """ 必须继承BaseMessage, ...
- Java基本数据类型总结二
Java 基本数据类型总结二 变量就是申请内存来存储值.也就是说,当创建变量的时候,需要在内存中申请空间. 内存管理系统根据变量的类型为变量分配存储空间,分配的空间只能用来储存该类型数据. 因此,通过 ...
- Ubuntu14.0.4系统如何获取root权限
Ubuntu14.0.4系统如何获取root权限 | 浏览:9684 | 更新:2014-08-21 10:38 7 分步阅读 本文主要讲解如何简单实用命令获取root权限 工具/原料 Ubuntu1 ...
- 机器学习tensorflow框架初试
本文来自网易云社区 作者:汪洋 前言 新手学习可以点击参考Google的教程.开始前,我们先在本地安装好 TensorFlow机器学习框架. 首先我们在本地window下安装好python环境,约定安 ...
- LC.exe已退出,代码为-1
解决方法就是把Properties文件下的license.licx给删除,重新编译,这样就可以了.
- Pascal小游戏 随机函数
一个被人写滥了的小程序,新手学习,Pascal By Chaobs 初学者可以用它来学习随机函数的运用,当然你完全可以自己写一个随机函数. var player1,player2:longint; ...
- Django笔记 —— MySQL安装
最近在学习Django,打算玩玩网页后台方面的东西,因为一直很好奇但却没怎么接触过.Django对我来说是一个全新的内容,思路想来也是全新的,或许并不能写得很明白,所以大家就凑合着看吧- 本篇笔记(其 ...
- 在SqlServer中通过SQL语句实现树状查询
CREATE PROCEDURE [dbo].[GetTree] @Id int AS BEGIN with cte as ( as lvl from Entity where Id = @Id un ...
- [译]12-spring依赖注入
每个java应用程序都是由多个类协作才最终生成了终端用户所使用的系统.当编写复杂java应用程序的时,类之间应尽 可能保持独立,因为这样更容易做到代码的重用,也有利于单元测试的开展.spring的依赖 ...