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.

分析:判断给定的字符串数组中满足在键盘中属于同一行的字符串,并返回该数组集合。注意给定的字符串是大小写混合的。

思路:将键盘上三行字符分别保存到三个字符串中,然后判断每个字符串是否都属于同一行。具体的看注释:

class Solution {
public String[] findWords(String[] words) {
// 将待对比的数组存入
String row1 = "qwertyuiop";
String row2 = "asdfghjkl";
String row3 = "zxcvbnm";
// 将符合条件的字符串放入ArrayList,之后转为字符串数组
ArrayList<String> new_words = new ArrayList<>();
for (int i = 0; i < words.length; i++) {
// 标记是否满足条件
boolean tt = false;
// 将要检查的字符串转成小写
String word = words[i].toLowerCase();
// 标记字符串属于第几行
int loop = 0;
for(int j = 0; j < word.length(); j++){
char ch = word.charAt(j);
if(j==0){
// 给初始行赋值
if(row1.indexOf(ch)!=-1)
loop=1;
if(row2.indexOf(ch)!=-1)
loop=2;
if(row3.indexOf(ch)!=-1)
loop=3;
}else {
// 若存在不同行,则进行标记,不满足条件,为TRUE
if(row1.indexOf(ch)!=-1&&loop!=1){
tt=true;
break;
}
if(row2.indexOf(ch)!=-1&&loop!=2){
tt=true;
break;
}
if(row3.indexOf(ch)!=-1&&loop!=3){
tt=true;
break;
}
}
}
if (tt) {
continue;
}
new_words.add(words[i]);
}
     // 将ArrayList转成字符串数组常用套路
String[] ss = new String[new_words.size()];
new_words.toArray(ss);
return ss;
}
}
 

Keyboard Row的更多相关文章

  1. 46. leetcode 500. Keyboard Row

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

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

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

  3. LeetCode——Keyboard Row

    LeetCode--Keyboard Row Question Given a List of words, return the words that can be typed using lett ...

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

  5. 500. Keyboard Row

    Given a List of words, return the words that can be typed using letters of alphabet on only one row' ...

  6. leetcode算法: Keyboard Row

    Given a List of words, return the words that can be typed using letters of alphabet on only one row' ...

  7. [LeetCode] Keyboard Row 键盘行

    Given a List of words, return the words that can be typed using letters of alphabet on only one row' ...

  8. [Swift]LeetCode500. 键盘行 | Keyboard Row

    Given a List of words, return the words that can be typed using letters of alphabet on only one row' ...

  9. LeetCode 500 Keyboard Row 解题报告

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

随机推荐

  1. 容器在 Weave 中如何通信和隔离?- 每天5分钟玩转 Docker 容器技术(65)

    上一节我们分析了 Weave 的网络结构,今天讨论 Weave 的连通和隔离特性. 首先在host2 执行如下命令: weave launch 192.168.56.104 这里必须指定 host1 ...

  2. Ubuntu & GitLab CI & Docker & ASP.NET Core 2.0 自动化发布和部署(2)

    上一篇:Ubuntu & GitLab CI & Docker & ASP.NET Core 2.0 自动化发布和部署(1) 服务器版本 Ubuntu 16.04 LTS. 本 ...

  3. 对The C programming language一书第6.6节代码的理解

    代码如下(基本与书中一致) 1 #include <stdio.h> 2 #include <string.h> 3 #include <ctype.h> 4 #i ...

  4. Java之JMX 详解

    详见:http://blog.yemou.net/article/query/info/tytfjhfascvhzxcyt194 一.JMX简介 JMX是一种JAVA的正式规范,它主要目的是让程序有被 ...

  5. Spring AOP 通过order来指定顺序

    详见:http://blog.yemou.net/article/query/info/tytfjhfascvhzxcyt398 Spring中的事务是通过aop来实现的,当我们自己写aop拦截的时候 ...

  6. 汇编指令-位置无关码(BL)与绝对位置码(LDR)(2)

    位置无关码,即该段代码无论放在内存的哪个地址,都能正确运行.究其原因,是因为代码里没有使用绝对地址,都是相对地址.  位置相关码,即它的地址与代码处于的位置相关,是绝对地址 BL :带链接分支跳转指令 ...

  7. 关于selenium IDE找不到元素

    selenium IDE ,明明存在元素,却找不到元素 ,报错Element not found 标签: seleniumselenium IDE自动化测试ide 2016-10-31 13:25 1 ...

  8. 转: 【Java并发编程】之十七:深入Java内存模型—内存操作规则总结

    转载请注明出处:http://blog.csdn.net/ns_code/article/details/17377197 主内存与工作内存 Java内存模型的主要目标是定义程序中各个变量的访问规则, ...

  9. 【Socket编程】Java中网络相关API的应用

    Java中网络相关API的应用 一.InetAddress类 InetAddress类用于标识网络上的硬件资源,表示互联网协议(IP)地址. InetAddress类没有构造方法,所以不能直接new出 ...

  10. 团队作业4——第一次项目冲刺(Alpha版本)4.27

    一.当天站立式会议照片 本次会议主要内容:组长给大家分配新一步任务,分别汇报目前所完成的内容,完善昨日的解压接口代码,并做测试修正. 二.每个人的工 三.燃尽图 横坐标:工作日,以天为单位,一共七天, ...