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. flex弹性布局学习

    一.介绍 flex( flexible box:弹性布局盒模型),是2009年w3c提出的一种可以简洁.快速弹性布局的属性.主要思想是给予容器控制内部元素高度和宽度的能力.目前已得到以下浏览器支持: ...

  2. spring配置datasource三种方式

    详见:http://blog.yemou.net/article/query/info/tytfjhfascvhzxcytp34 spring配置datasource三种方式 1.使用org.spri ...

  3. AFN和SDWebImage请求网络图片的一点问题

    问题1.AFN 处理有关图片相关的请求的问题 在使用AFN Post网络图片的时候发现NSLocalizedDescription=Request failed: unacceptable conte ...

  4. NOPI读xls文件写到txt中(NPOI系列二)

    private void button2_Click(object sender, EventArgs e) { StringBuilder sb = new StringBuilder(); //找 ...

  5. ★浅谈Spanking情节

  6. ★10 个实用技巧,让Finder带你飞~

    10 个实用技巧,让 Finder 带你飞 Finder 是 Mac 电脑的系统程序,有的功能类似 Windows 的资源管理器.它是我们打开 Mac 首先见到的「笑脸」,有了它,我们可以组织和使用 ...

  7. 转:【Java集合源码剖析】Vector源码剖析

    转载请注明出处:http://blog.csdn.net/ns_code/article/details/35793865   Vector简介 Vector也是基于数组实现的,是一个动态数组,其容量 ...

  8. Matlab生成.exe可执行程序

    由于在教学过程中需要演示Matlab程序,而教学机又未安装Matlab程序,因此有必要将Matlab程序生成.exe可执行程序,便于直接执行. 在Matlab中提供了Complier,可直接使用. ( ...

  9. 201521123091 《Java程序设计》第4周学习总结

    Java 第二周总结 第四周的作业. 目录 1.本章学习总结 2.Java Q&A 3.使用码云管理Java代码 4.PTA实验 1.本章学习总结 1.1 尝试使用思维导图总结有关继承的知识点 ...

  10. 201521123054《Java程序设计》第1周学习总结

    #1. 本章学习总结 你对于本章知识的学习总结 本章我们学习了各种java相关文件的使用,能够进行基本的程序操作: 学会使用博客.码云与PTA管理java: #2. 书面作业 1.为什么java程序可 ...