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

思路: 其实就是遍历每一个字符串,将键盘中的每一行分别放到set中,判断字符串的每一个字符是否都在同一个set中,用count来计数,每次count清零,具体思路见注释。

代码:

class Solution {
public:vector<string> findWords(vector<string>& words) {vector<string> res;//保存结果vector<set<char> > key;//将键盘的字符以行放入key中,每一行为一个setkey.push_back(set<char>({'Q', 'W', 'E','R','T','Y','U','I','O','P'}));key.push_back(set<char>({'A', 'S', 'D', 'F', 'G', 'H', 'J', 'K', 'L'}));key.push_back(set<char>({'Z','X','C','V','B','N','M'}));int size = words.size();for (int i = 0; i < size; ++i){//遍历每一个字符串int len = words[i].length();//取每一个字符串的长度int count = 0;for (int j = 0; j < 3; ++j){count = 0;//每次count置0for (int k = 0; k < len; ++k){if (key[j].count(toupper(words[i][k]))){//如果这个字符串的当前字符在第j个key中++count;//count加1}}if (count&&count != len||count==len){//如果count不为0且count不等于len 或者 count直接等于lenbreak;//跳出循环}}if (count == len){//如果count等于len,则这个字符串符合要求res.push_back(words[i]);}}return res;}
};

结果: 0ms

LeetCode 500. Keyboard Row相关推荐

  1. Leetcode PHP题解--D25 500. Keyboard Row

    500. Keyboard Row 题目链接 500. Keyboard Row 题目分析 给定一个字符串数组,返回那些所出现的字母在QWERTY键盘中同一行的字符串. 例如,单词hello中,字母h ...

  2. 【leetcode】500. Keyboard Row

    问题描述: Given a List of words, return the words that can be typed using letters of alphabet on only on ...

  3. LeetCode之Keyboard Row

    1.题目: Given a List of words, return the words that can be typed using letters of alphabet on only on ...

  4. 500. Keyboard Row

    此题要求在input string array中找出能被一行键盘打出来的string,很有意思的一题,有很多amazing的解法: Solution 1:一行函数解爆它 public String[] ...

  5. LeetCode Keyboard Row

    原题链接在这里:https://leetcode.com/problems/keyboard-row/#/description 题目: Given a List of words, return t ...

  6. C#LeetCode刷题之#500-键盘行(Keyboard Row)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3796 访问. 给定一个单词列表,只返回可以使用在键盘同一行的字母 ...

  7. LeetCode - 500 - 键盘行(keyboard-row)

    一 目录 不折腾的前端,和咸鱼有什么区别 目录 一 目录 二 前言 三 解题及测试 四 LeetCode Submit 五 解题思路 六 进一步思考 二 前言 难度:简单 涉及知识:哈希表 题目地址: ...

  8. Leetcode 500题AC的刷题总结(C与C++)

    文章目录 前引闲聊 500AC达成截图 + 力扣刷题每日频率 前引闲聊 哈哈 又到了我的闲聊时间了 与其这篇说是500题AC的总结 不如说是我的闲聊时间 我记得上一篇关于Leetcode AC记录 还 ...

  9. leetcode 500. 键盘行(Java版)

    题目 https://leetcode-cn.com/problems/keyboard-row/ 题解 对于每一个单词,先确定所在键盘行数,然后验证后面的字母是否在同一行. class Soluti ...

最新文章

  1. 解决ubuntu开机进入grub界面的问题
  2. 浅析影响网站空间选择的三大因素
  3. flood fill算法
  4. linux下c中嵌套正则表达式
  5. Docker容器相关命令
  6. python合并csv文件_PYTHON合并CSV文件的实践
  7. HTML5的绝活:巧用Canvas制作会动的时钟
  8. boost::log模块实现logging旋转文本文件的示例
  9. 资深大牛推荐学习路线建议
  10. 【vue项目使用echarts实现区域地图绘制,且可点击单独区域】
  11. _mount_vendor
  12. 鸿蒙 自研内核 core b,华为平板将首次搭载鸿蒙OS 2.0系统:首次自研内核与构架...
  13. 7.1 API:GaussianMixture
  14. 手机如何远程连接服务器
  15. 01惯性导航常用坐标系与地球参考椭球
  16. 再见,2018俄罗斯世界杯!
  17. 机器学习:PageRank
  18. android 接电话 返回后黑屏,安卓手机打电话黑屏怎么办?解决安卓接电话黑屏
  19. 【扫盲】硬盘接口『 SATA 、 mSATA 、 m.2 ( NGFF )、PCI-E ( PICe 、 PCI Express ) 』及其协议 『 NVMe 、 AHCI 』
  20. 基于Open vSwitch的传统限速和SDN限速--实验

热门文章

  1. jsp jquery ajax请求,JSP+jquery使用ajax方式调用json的实现方法
  2. 操作mysql_MySQL 事务操作
  3. OpenGL:glMatrixMode()
  4. 网站颜色变黑白的CSS代码,Chrome、火狐、IE
  5. 小程序助手多功能微信小程序反编译工具
  6. asp留言板源码-XYCMS留言板 v8.0
  7. c语言delay_单片机模拟流星雨,电路图和程序,一起来感受下,其实C语言不难...
  8. ie input兼容 vue_Vue项目与IE11兼容
  9. Diaspora-v1.3.0 素锦WordPress博客主
  10. Linux下删除mysql实例,linux下完全删除mysql