LeetCode Keyboard Row
原题链接在这里:https://leetcode.com/problems/keyboard-row/#/description
题目:
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.
题解:
把在同row的char标记成同index存在HashMap<Character, Integer> hm中.
然后判定word中的每一个char是否有相同的index.
Time Complexity: O(n), n是words中每个word的char总和.
Space: O(1). hm size, regardless res size.
AC Java:
1 public class Solution { 2 public String[] findWords(String[] words) { 3 if(words == null || words.length == 0){ 4 return words; 5 } 6 7 String [] keyboard = {"qwertyuiop", "asdfghjkl", "zxcvbnm"}; 8 HashMap<Character, Integer> hm = new HashMap<Character, Integer>(); 9 for(int i = 0; i<keyboard.length; i++){ 10 for(char c : keyboard[i].toCharArray()){ 11 hm.put(c, i); 12 } 13 } 14 15 List<String> res = new ArrayList<String>(); 16 for(String word : words){ 17 if(word == null || word.length() == 0){ 18 continue; 19 } 20 int ind = hm.get(word.toLowerCase().charAt(0)); 21 for(char c : word.toLowerCase().toCharArray()){ 22 if(ind != hm.get(c)){ 23 ind = -1; 24 } 25 } 26 if(ind != -1){ 27 res.add(word); 28 } 29 } 30 31 return res.toArray(new String[0]); 32 } 33 }
转载于:https://www.cnblogs.com/Dylan-Java-NYC/p/6637828.html
LeetCode Keyboard Row相关推荐
- Leetcode PHP题解--D25 500. Keyboard Row
500. Keyboard Row 题目链接 500. Keyboard Row 题目分析 给定一个字符串数组,返回那些所出现的字母在QWERTY键盘中同一行的字符串. 例如,单词hello中,字母h ...
- 【leetcode】500. Keyboard Row
问题描述: Given a List of words, return the words that can be typed using letters of alphabet on only on ...
- LeetCode之Keyboard Row
1.题目: Given a List of words, return the words that can be typed using letters of alphabet on only on ...
- LeetCode 500. Keyboard Row
Given a List of words, return the words that can be typed using letters of alphabet on only one row' ...
- C#LeetCode刷题之#500-键盘行(Keyboard Row)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3796 访问. 给定一个单词列表,只返回可以使用在键盘同一行的字母 ...
- 500. Keyboard Row
此题要求在input string array中找出能被一行键盘打出来的string,很有意思的一题,有很多amazing的解法: Solution 1:一行函数解爆它 public String[] ...
- C#LeetCode刷题-哈希表
哈希表篇 # 题名 刷题 通过率 难度 1 两数之和 C#LeetCode刷题之#1-两数之和(Two Sum) 42.8% 简单 3 无重复字符的最长子串 24.2% 中等 18 四数之和 ...
- LeetCode 简单算法题
使用Nodejs 抓取的LeetCode 简单算法题 一步一步来,先攻破所有简单的题目,有些题目不适合使用JS解决,请自行斟酌 Letcode 简单题汇总 104. Maximum Depth of ...
- 【Leetcode】精选算法top200道(二)
二.中等 339.嵌套列表权重和 给定一个嵌套的整数列表 nestedList ,每个元素要么是整数,要么是列表.同时,列表中元素同样也可以是整数或者是另一个列表. 整数的 深度 是其在列表内部的嵌套 ...
最新文章
- Linux运维工程师可是很吃香的
- Spring Boot 2.x基础教程:JSR-303实现请求参数校验
- kafka安装_安装Kafka
- Parse a document from a String
- 关于bochs用X11启动的说明
- Visual Studio 2010 中的 SharePoint 开发
- TCP为何采用三次握手来建立连接,若采用二次握手可以吗
- Linux 的 chmod 命令,对一个目录及其子目录所有文件添加权限
- 2021知识类内容研究报告
- 10个必会的 PyCharm 技巧,附高清大图
- linux不停机释放日志,Kafka在线修改topic日志保存时长(不停机,不重启)
- 清华大学计算机夏文韬,太猛了--南京外国语学校2007届高三毕业生去向
- Windows 7 频繁提示:计算机的内存不足
- UNITY_iOS_接入微信登录及分享微信
- Java、JSP 基于微信小程序 宠物在线医疗管理系统的设计与实现
- 数据库设计-多级栏目(标题)分类设计
- 平面设计中的插画设计技巧
- [fyne] build constraints exclude all Go files in
- java 方法规范_Java规范推荐
- Android IO流程你真的清楚了吗|硬核科普