作者: 负雪明烛
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)的更多相关文章

  1. LeetCode 500 Keyboard Row 解题报告

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

  2. 【LeetCode】120. Triangle 解题报告(Python)

    [LeetCode]120. Triangle 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址htt ...

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

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

  4. 46. leetcode 500. Keyboard Row

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

  5. 【LeetCode】383. Ransom Note 解题报告(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Java解法 Python解法 日期 [LeetCo ...

  6. 【LeetCode】575. Distribute Candies 解题报告(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Java解法 Python解法 日期 题目地址:ht ...

  7. 【LeetCode】515. Find Largest Value in Each Tree Row 解题报告(Python & C++ & Java)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 BFS DFS 日期 题目地址:https://le ...

  8. 【LeetCode】237. Delete Node in a Linked List 解题报告 (Java&Python&C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 设置当前节点的值为下一个 日期 [LeetCode] ...

  9. 【LeetCode】349. Intersection of Two Arrays 解题报告(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:Java解法,HashSet 方法二:Pyt ...

随机推荐

  1. 拒绝恶意同构ssh登陆服务器脚本

    #!/bin/bash #Deny specified IP access #IP:who is fail to login sever SECURE_LOG=/var/log/secure #通过s ...

  2. Linux关机/重启/用户切换/注销

    目录 1. 关机/重启命令 2. 用户切换/注销 2.1 基本说明 2.2 切换用户 2.3 注销用户 1. 关机/重启命令 # shutdown命令 shutdown -h now # 立即关机 s ...

  3. PowerToys插件扩展(类似Alfred)

    在mac系统除了自带的Spotlight还有一个很好用的工具叫Alfred image 在windows系统也有一个很好用的工具叫PowerToys,是微软的一个开源项目 image https:// ...

  4. CSS3单行文本两端对齐

    CSS3实现单行文本两端对齐 p { height: 24px; text-align: justify; text-last-align: justify; } p::after { display ...

  5. 3 - 简单了解一下springboot中的yml语法 和 使用yml赋值

    1.简单了解yml语法 2.使用yml给实体类赋值 准备工作:导入依赖 <!-- 这个jar包就是为了实体类中使用@ConfigurationProperties(prefix = " ...

  6. javaWeb - 3 — JSP (技术已淘汰)— 更新完毕

    1.jsp 在servlet中说过java中前端到后台有两条路线嘛 后台 <------ ajax.json <--------- 前端 后台 <------ jsp( EL.JST ...

  7. Learning Spark中文版--第四章--使用键值对(1)

      本章介绍了如何使用键值对RDD,Spark中很多操作都基于此数据类型.键值对RDD通常在聚合操作中使用,而且我们经常做一些初始的ETL(extract(提取),transform(转换)和load ...

  8. Hadoop fs.copyToLocalFile错误

    fs.copyToLocalFile(new Path("/study1/1.txt"), new Path("C:/Users/Administrator/Deskto ...

  9. 如何通过 User-Agent 识别百度蜘蛛

    如果有大量的百度蜘蛛抓取网站就需要注意了:有可能是其他爬虫伪造百度蜘蛛恶意抓取网站. 如果遇到这种情况,这时候就需要查看日志来确定是不是真正的百度蜘蛛(baidu spider).搜索引擎蜘蛛.用户访 ...

  10. 【Linux】【Shell】【Basic】变量与数据类型

    1. 变量: 1.1. 局部变量:作用域是函数的生命周期:在函数结束时被自动销毁: 定义局部变量的方法:local VARIABLE=VALUE 1.2. 本地变量:作用域是运行脚本的shell进程的 ...