Keyboard Row
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.
分析:判断给定的字符串数组中满足在键盘中属于同一行的字符串,并返回该数组集合。注意给定的字符串是大小写混合的。
思路:将键盘上三行字符分别保存到三个字符串中,然后判断每个字符串是否都属于同一行。具体的看注释:
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的更多相关文章
- 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#500. Keyboard Row(键盘行)
题目描述 给定一个单词列表,只返回可以使用在键盘同一行的字母打印出来的单词.键盘如下图所示. 示例1: 输入: ["Hello", "Alaska", &quo ...
- LeetCode——Keyboard Row
LeetCode--Keyboard Row Question Given a List of words, return the words that can be typed using lett ...
- 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 ...
- 500. Keyboard Row
Given a List of words, return the words that can be typed using letters of alphabet on only one row' ...
- leetcode算法: Keyboard Row
Given a List of words, return the words that can be typed using letters of alphabet on only one row' ...
- [LeetCode] Keyboard Row 键盘行
Given a List of words, return the words that can be typed using letters of alphabet on only one row' ...
- [Swift]LeetCode500. 键盘行 | Keyboard Row
Given a List of words, return the words that can be typed using letters of alphabet on only one row' ...
- LeetCode 500 Keyboard Row 解题报告
题目要求 Given a List of words, return the words that can be typed using letters of alphabet on only one ...
随机推荐
- MyBatis框架——关系映射(一对多、多对多、多对一查询)
关系映射 一.映射(多)对一.(一)对一的关联关系 1).使用列的别名 ①.若不关联数据表,则可以得到关联对象的id属性 ②.若还希望得到关联对象的其它属性.则必须关联其它的数据表 1.创建表: 员工 ...
- Ibatis insert语句插入null引发的错误
公司使用的orm框架为ibatis,其中默认的insert语句一直都是这样写的: <insert id="insert" parameterClass="activ ...
- 谈一谈EasyUI中TreeGrid的过滤功能
写在最前面 这个星期一直在纠结easyui的treegrid的过滤功能,原因呢,自然是项目中一个莫名奇妙的需求. easyui虽说是后端程序员的前端框架,但是说句实话,除去api,让我直接写里面的节点 ...
- python--对于装饰器的理解
1.首先,有个原来写好的函数,完成一定的功能,比如下面的,就打印一句话(某程序被调用).简单点,容易帮我们想清楚程序是怎么执行的. ''' 原函数 ''' def fun1(): print(&quo ...
- Java语言课程设计——博客作业教学数据分析系统(201521123107 张翔)
#Java语言课程设计--博客作业教学数据分析系统(个人博客) 1.团队课程设计博客链接 [博客作业教学数据分析系统(From:网络五条狗)](http://www.cnblogs.com/fanta ...
- 团队作业4——第一次项目冲刺(Alpha版本) 2
一.Daily Scrum Meeting照片 二.燃尽图 三.项目进展 1.完成了大部分查重算法的书写. 余弦查重算法 2.其他非主页面的部分设计. 查重过程的显示页面 四.困难与问题 1.算法是程 ...
- 201521123039 《java程序设计》第四周学习总结
1. 本周学习总结 总结: 1.提到类的继承就会想到继承层次的问题,一般我们都将子类和父类共同的特征放到父类中,将具有特殊用途的方法放在子类中,这样可以有效避免代码冗余. 2.覆盖与重载是不同的概念, ...
- 201521123052《Java程序设计》第9周学习总结
1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结异常相关内容. 2. 书面作业 本次PTA作业题集异常 1.常用异常 题目5-1 1.1 截图你的提交结果(出现学号) 1.2 自己 ...
- 201521123118《java与程序设计》第14周学习总结
1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结多数据库相关内容. 2. 书面作业 1. MySQL数据库基本操作 建立数据库,将自己的姓名.学号作为一条记录插入.(截图,需出现自 ...
- Java项目生成Jar文件
打开 Jar 文件向导 Jar 文件向导可用于将项目导出为可运行的 jar 包. 打开向导的步骤为: 在 Package Explorer 中选择你要导出的项目内容.如果你要导出项目中所有的类和资源, ...