问题描述:

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:

  1. Input: ["Hello", "Alaska", "Dad", "Peace"]
  2. Output: ["Alaska", "Dad"]

Note:

  1. You may use one character in the keyboard more than once.
  2. You may assume the input string will only contain letters of alphabet.

解法一:

常规解法,用unordered_set存储每一行的字母,依次寻找判断。

  1. class Solution {
  2. public:
  3. vector<string> findWords(vector<string>& words) {
  4. unordered_set<char> row1 {'q', 'w', 'e', 'r', 't', 'y','u', 'i', 'o', 'p'};
  5. unordered_set<char> row2 {'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l'};
  6. unordered_set<char> row3 { 'z', 'x', 'c', 'v', 'b' ,'n', 'm'};
  7. vector<unordered_set<char>> rows {row1, row2, row3};
  8.  
  9. vector<string> validWords;
  10. for(int i=; i<words.size(); ++i){
  11. int row=;
  12.  
  13. for(int k=; k<; ++k){
  14. if(rows[k].count((char)tolower(words[i][])) > ) row = k;
  15. }
  16.  
  17. validWords.push_back(words[i]);
  18. for(int j=; j<words[i].size(); ++j){
  19. if(rows[row].count((char)tolower(words[i][j])) == ){
  20. validWords.pop_back();
  21. break;
  22. }
  23. }
  24.  
  25. }
  26. return validWords;
  27. }
  28. };

解法二:

这种解法比较有启发性,看起来很有意思。

  1. class Solution {
  2. public:
  3. vector<string> findWords(vector<string>& words)
  4. {
  5. vector<string> res;
  6.  
  7. for(auto str : words)
  8. {
  9. bool r1 = str.find_first_of("QWERTYUIOPqwertyuiop") == string::npos ? false : true;
  10. bool r2 = str.find_first_of("ASDFGHJKLasdfghjkl") == string::npos ? false : true;
  11. bool r3 = str.find_first_of("ZXCVBNMzxcvbnm") == string::npos ? false : true;
  12.  
  13. if(r1 + r2 + r3 == )
  14. res.push_back(str);
  15. }
  16.  
  17. return res;
  18. }
  19.  
  20. };

【leetcode】500. Keyboard Row的更多相关文章

  1. 【LeetCode】500. Keyboard Row 解题报告(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 暴力解 字典 + set 日期 题目地址:https ...

  2. 【LeetCode】623. Add One Row to Tree 解题报告(Python)

    [LeetCode]623. Add One Row to Tree 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problem ...

  3. 46. leetcode 500. Keyboard Row

    500. Keyboard Row Given a List of words, return the words that can be typed using letters of alphabe ...

  4. Leetcode#500. Keyboard Row(键盘行)

    题目描述 给定一个单词列表,只返回可以使用在键盘同一行的字母打印出来的单词.键盘如下图所示. 示例1: 输入: ["Hello", "Alaska", &quo ...

  5. 【leetcode】500_Keyboard Row

    problem 500. Keyboard Row 题意:判断给出的某个单词是否可以使用键盘上的某一行字母type得到: 注意大小写的转换: solution1: 使用set保存三行字符,查看每个字符 ...

  6. 【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 ...

  7. 【LeetCode】 454、四数之和 II

    题目等级:4Sum II(Medium) 题目描述: Given four lists A, B, C, D of integer values, compute how many tuples (i ...

  8. 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 ...

  9. 【LeetCode】树(共94题)

    [94]Binary Tree Inorder Traversal [95]Unique Binary Search Trees II (2018年11月14日,算法群) 给了一个 n,返回结点是 1 ...

随机推荐

  1. Android面试收集录15 Android Bitmap压缩策略

    一.为什么Bitmap需要高效加载? 现在的高清大图,动辄就要好几M,而Android对单个应用所施加的内存限制,只有小几十M,如16M,这导致加载Bitmap的时候很容易出现内存溢出.如下异常信息, ...

  2. python面向对象的约束和自定义异常

    基于人为来约束: 即人为主动抛出异常 class BaseMessage(object): def send(self,x1): """ 必须继承BaseMessage, ...

  3. Java基本数据类型总结二

    Java 基本数据类型总结二 变量就是申请内存来存储值.也就是说,当创建变量的时候,需要在内存中申请空间. 内存管理系统根据变量的类型为变量分配存储空间,分配的空间只能用来储存该类型数据. 因此,通过 ...

  4. Ubuntu14.0.4系统如何获取root权限

    Ubuntu14.0.4系统如何获取root权限 | 浏览:9684 | 更新:2014-08-21 10:38 7 分步阅读 本文主要讲解如何简单实用命令获取root权限 工具/原料 Ubuntu1 ...

  5. 机器学习tensorflow框架初试

    本文来自网易云社区 作者:汪洋 前言 新手学习可以点击参考Google的教程.开始前,我们先在本地安装好 TensorFlow机器学习框架. 首先我们在本地window下安装好python环境,约定安 ...

  6. LC.exe已退出,代码为-1

    解决方法就是把Properties文件下的license.licx给删除,重新编译,这样就可以了.

  7. Pascal小游戏 随机函数

    一个被人写滥了的小程序,新手学习,Pascal By Chaobs 初学者可以用它来学习随机函数的运用,当然你完全可以自己写一个随机函数. var   player1,player2:longint; ...

  8. Django笔记 —— MySQL安装

    最近在学习Django,打算玩玩网页后台方面的东西,因为一直很好奇但却没怎么接触过.Django对我来说是一个全新的内容,思路想来也是全新的,或许并不能写得很明白,所以大家就凑合着看吧- 本篇笔记(其 ...

  9. 在SqlServer中通过SQL语句实现树状查询

    CREATE PROCEDURE [dbo].[GetTree] @Id int AS BEGIN with cte as ( as lvl from Entity where Id = @Id un ...

  10. [译]12-spring依赖注入

    每个java应用程序都是由多个类协作才最终生成了终端用户所使用的系统.当编写复杂java应用程序的时,类之间应尽 可能保持独立,因为这样更容易做到代码的重用,也有利于单元测试的开展.spring的依赖 ...