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 ...
随机推荐
- Ext:ComboBox实战
var gsCombo = new Ext.form.ComboBox({ id:'combo', hiddenName:'value', fieldLabel:'下拉框', triggerActio ...
- informatica9 安装下载,安装教程 介质(文章和视频教程)(csdn讲师:Array)
Informatica学习: 参考文献:视频参考地址:http://edu.csdn.net/course/detail/5034,ETL之informatica9通关班(第二期) 1.安装介质的获 ...
- setTimeout和setInterval不容易注意到的一些细节
今天没事翻了翻JS高程,看到了setTimeout部分有这么一句话:调用setTimeout()之后,该方法会返回一个数值ID,表示超时调用.这个超时调用ID是计划执行代码的唯一标识符,可以通过它来取 ...
- jQuery插件——ajax
一.ajax请求 1.load(url, [data], [callback]) 概述:加载远程的HTML文件代码,并插入到指定的DOM节点中. 参数:url:待装入 HTML 网页网址. data: ...
- MITNIK ATTACK
Https 443 http 80 TCP/IP 协议栈:将数据封装包头 传输层报头 Ack回复确认位 FIN结束位 SIN 开始位 RST 重置位 Seq 序号位 网络层报头 目的地址 原地址 报文 ...
- 搭建自己的CA服务 - OpenSSL CA 实战
当前网络安全事故不断,如何提升系统安全性是一个系统上线之前必须考虑的重点DFx特性之一.在提升系统安全性的方法中, 给每个端口(通道)加上SSL协议是最通用和有效的一种. 使用SSL就必须要有证书,在 ...
- 201521123065《java程序设计》第8周学习总结
1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结集合与泛型相关内容. 1.2 选做:收集你认为有用的代码片段 1.Map通过get(key)来获取值: 2.HashMap和Hash ...
- 201521123054《Java程序设计》第1周学习总结
#1. 本章学习总结 你对于本章知识的学习总结 本章我们学习了各种java相关文件的使用,能够进行基本的程序操作: 学会使用博客.码云与PTA管理java: #2. 书面作业 1.为什么java程序可 ...
- sql+PHP基础+面向对象基础简单总结
一.MYSQL 1.配置MySql 第一步安装服务器(apache). 第二部安装MySql界面程序 2 ...
- windows 2008 R2操作系统上使用iis服务运行php和mysql数据库的网站遇到的验证码不显示问题?
1,本地运行正常,路径没问题. 2,GD2.dll开启 3,IIS服务正常开启,并启用父路径为True 4,检查是否IIS服务中FSO权限问题 开始 -> 程序 -> 管理工具 -> ...