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 ...
随机推荐
- .net分布式压力测试工具(Beetle.DT)
肯定有人会问为什么会写这样一个开源工具?和现有的有什么差别?不过对于一个程序员来说写东西还真不需要理由的:),主要原因是工作有点闲(开玩笑),不过说实话一个程员怎可能会停止写代码呢(作为一个奔4的程序 ...
- ThinkPHP自定义分页模板
TpPageHelper.php <?php namespace tool; use think\Paginator; class TpPageHelper extends Paginator ...
- python利用urllib实现的爬取京东网站商品图片的爬虫
本例程使用urlib实现的,基于python2.7版本,采用beautifulsoup进行网页分析,没有第三方库的应该安装上之后才能运行,我用的IDE是pycharm,闲话少说,直接上代码! # -* ...
- mysql数据类型double和decimal区别详解
详见:http://blog.yemou.net/article/query/info/tytfjhfascvhzxcyt336 实数是带有小数部分的数字.然而,它们不只是为了存储小数部分,也可以使用 ...
- Android微信登录、分享、支付
转载需要著名出处: http://blog.csdn.net/lowprofile_coding/article/details/78004224 之前写过微信登录分享支付第一版: http://bl ...
- 自制MPLS解决路由黑洞实验
利用mpls解决BGP路由黑洞配置命令全解析 --By Jim 什么是BGP路由黑洞? BGP规定无论路由器是否启动bgp都要无条件地转发BGP消息和更新包(凌驾于IGP之上),违背了IGP" ...
- unity调用c++ dll方法介绍
摘要 unity用的很普遍,现在很多代码还是用c++写的,需要用unity去调用c++的代码.这里介绍了一种unity调用c++ dll的方法,希望有所帮助. 我采用的软件是Visual Studio ...
- 超级简单实用的前端必备技能-javascript-全屏滚动插件
fullPage.js fullPage.js是一个基于jQuery的全屏滚动插件,它能够很方便.很轻松的制作出全屏网站 本章内容将详细介绍Android事件的具体处理及常见事件. 主要功能 支持 ...
- 团队作业8——第二次项目冲刺(Beta阶段)Day1--5.18
1.展开站立式会议: 会议内容:①团队里的新成员做简单的自我介绍. ②讨论每天的任务分配问题. ③提出一个严格要求:博客一定要及时发布,并及时提交作业,遇到困难可以提出来一起解决. ④就现阶段的项目进 ...
- 201521123004 《Java程序设计》第13周学习总结
1. 本周学习总结 以你喜欢的方式(思维导图.OneNote或其他)归纳总结多网络相关内容. 协议.IP.域名.端口号 协议:网络中为了进行数据交换(通信)而建立的规则.标准或约定(=语义+语法+规则 ...