【LeetCode】500. Keyboard Row 解题报告(Java & Python)
作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/
题目地址: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 :
Input: ["Hello", "Alaska", "Dad", "Peace"]
Output: ["Alaska", "Dad"]
题目大意
判断那些字符串能使用键盘中的其中一行就能全部拼出来。
解题方法
暴力解
暴力解决了。分别把三行弄在三个数组里,对于每个单词每个字母都去循环,数在三行中的个数分别多少。如果这个单词能在一张中打出来完,那么说明由某一行的个数为1,其他行都为0.
注意字符串数组的写法。
public class Solution {
public String[] findWords(String[] words) {
char []arr1 = new char[]{'q','w','e','r','t','y','u','i','o','p','Q','W','E','R','T','Y','U','I','O','P'};
char []arr2 = new char[]{'a','s','d','f','g','h','j','k','l','A','S','D','F','G','H','J','K','L'};
char []arr3 = new char[]{'z','x','c','v','b','n','m','Z','X','C','V','B','N','M'};
List<String> ans = new ArrayList<String>();
for(String word: words){
int count1 = 0, count2 = 0, count3 = 0;
for(int i =0; i < word.length(); i++){
for(int j =0; j < arr1.length; j++){
if(word.charAt(i) == arr1[j]){
count1++;
}
}
for(int j =0; j < arr2.length; j++){
if(word.charAt(i) == arr2[j]){
count2++;
}
}
for(int j =0; j < arr3.length; j++){
if(word.charAt(i) == arr3[j]){
count3++;
}
}
}
if((count1 != 0 && count2 == 0 && count3 == 0)
||(count1 == 0 && count2 != 0 && count3 == 0)
||(count1 == 0 && count2 == 0 && count3 != 0)){
ans.add(word);
}
}
String []answer = new String[ans.size()];
for(int i =0; i < ans.size(); i ++){
answer[i] = ans.get(i);
}
return answer;
}
}
字典 + set
二刷,Python。
使用字典来保存字符串在哪一行,然后遍历每个字符串,看它所有的字符在哪几行,用set对行数去重,如果set的结果是1,说明可以使用一行就求解出来。
class Solution:
def findWords(self, words):
"""
:type words: List[str]
:rtype: List[str]
"""
rowdict = {}
for c in "qwertyuiopQWERTYUIOP":
rowdict[c] = 1
for c in "asdfghjklASDFGHJKL":
rowdict[c] = 2
for c in "zxcvbnmZXCVBNM":
rowdict[c] = 3
res = []
for word in words:
if len(set(rowdict[c] for c in word)) == 1:
res.append(word)
return res
日期
2017 年 4 月 2 日
2018 年 11 月 6 日 —— 腰酸背痛要废了
【LeetCode】500. Keyboard Row 解题报告(Java & Python)的更多相关文章
- LeetCode 500 Keyboard Row 解题报告
题目要求 Given a List of words, return the words that can be typed using letters of alphabet on only one ...
- 【LeetCode】120. Triangle 解题报告(Python)
[LeetCode]120. Triangle 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址htt ...
- Leetcode#500. Keyboard Row(键盘行)
题目描述 给定一个单词列表,只返回可以使用在键盘同一行的字母打印出来的单词.键盘如下图所示. 示例1: 输入: ["Hello", "Alaska", &quo ...
- 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】383. Ransom Note 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Java解法 Python解法 日期 [LeetCo ...
- 【LeetCode】575. Distribute Candies 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Java解法 Python解法 日期 题目地址:ht ...
- 【LeetCode】515. Find Largest Value in Each Tree Row 解题报告(Python & C++ & Java)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 BFS DFS 日期 题目地址:https://le ...
- 【LeetCode】237. Delete Node in a Linked List 解题报告 (Java&Python&C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 设置当前节点的值为下一个 日期 [LeetCode] ...
- 【LeetCode】349. Intersection of Two Arrays 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:Java解法,HashSet 方法二:Pyt ...
随机推荐
- hash_map,map,unordered_map效率
利用unordered_map代替hash_map 实验环境 操作系统 fedora9 编译器版本 gcc4.3 实验方式 各种map使用插入和查找,比较速度和相关性能 代码 参考代码 下面测试说明了 ...
- 日常Java测试第一段 2021/11/12
课堂测试一 package word_show;import java.io.BufferedReader;import java.io.FileNotFoundException;import ja ...
- Vue2全家桶+Element搭建的PC端在线音乐网站
目录 1,前言 2,已有功能 3,使用 4,目录结构 5,页面效果 登录页 首页 排行榜 歌单列表 歌单详情 歌手列表 歌手详情 MV列表 MV详情 搜索页 播放器 1,前言 项目基于Vue2全家桶及 ...
- 大厂高频面试题Spring Bean生命周期最详解
Spring作为当前Java最流行.最强大的轻量级框架.Spring Bean的生命周期也是面试高频题,了解Spring Bean周期也能更好地帮助我们解决日常开发中的问题.程序员应该都知道Sprin ...
- Scala(六)【模式匹配】
目录 一.基本语法 二.匹配固定值 三.守卫 四.匹配类型 五.匹配集合 1.Array 2.List 3.元祖 4.对象和样例类 六.偏函数 七.赋值匹配 八.for循环匹配 一.基本语法 在匹配某 ...
- Angular @ViewChild,Angular 中的 dom 操作
Angular 中的 dom 操作(原生 js) ngAfterViewInit(){ var boxDom:any=document.getElementById('box'); boxDom.st ...
- E面波导和H面波导的问题
我感觉与窄壁平行是E面,反之为H面.通常E面(窄面)是指与电场方向平行的方向图切面(窄面):H面(宽面)是指与磁场方向平行的方向图切面(宽面).E面的意思是... ElevationH面的意思是... ...
- 【STM8】STM8S介绍(编程环境、烧录、芯片内容)(Vcap需要一个电容接地)
这篇博客的介绍大纲 [1]我使用的开发板和烧录器 [2]编程环境 [3]烧录软件和界面 [4]芯片内容 [1]我使用的开发板和烧录器 首先,我用的是STM8S003F3P6这款开发板,淘宝上就有了,5 ...
- SpringMVC详细实例
一.SpringMVC基础入门,创建一个HelloWorld程序 1.首先,导入SpringMVC需要的jar包. 2.添加Web.xml配置文件中关于SpringMVC的配置 1 2 3 4 5 6 ...
- Spring标签库
spring提供了两个标签库文件:spring-form.tld(表单标签库,用于输出HTML表单) spring.tld(基础标签库,用于Spring数据绑定等) 使用步骤: 1,配置表单标签库, ...