500. Keyboard Row
来源:互联网 发布:手机屏幕直播录像软件 编辑:程序博客网 时间:2024/05/17 01:05
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.
这道题思路比较简单,只是时间复杂度有点高,代码如下:
public String[] findWords(String[] words) {
ArrayList<String> resultList = new ArrayList<String>();
// 特殊情况处理
if(words == null){
return null;
}
String[] temp = {"qwertyuiopQWERTYUIOP", "asdfghjklASDFGHJKL", "zxcvbnmZXCVBNM"};
HashMap<Character, Integer> hashMap = new HashMap<Character, Integer>();
for(int i = 0; i < temp.length; i++){
for(int j = 0; j < temp[i].length(); j++){
hashMap.put(temp[i].charAt(j), i);
}
}
for(int i = 0; i < words.length; i++){
String test = words[i];
boolean flag = true;
int index = hashMap.get(test.charAt(0));
for(int j = 1; j < test.length(); j++){
if(hashMap.get(test.charAt(j)) != index){
flag = false;
break;
}
}
if(flag){
resultList.add(test);
}
}
String[] result = new String[resultList.size()];
for(int i = 0; i < result.length; i++){
result[i] = resultList.get(i);
}
return result;
}
ArrayList<String> resultList = new ArrayList<String>();
// 特殊情况处理
if(words == null){
return null;
}
String[] temp = {"qwertyuiopQWERTYUIOP", "asdfghjklASDFGHJKL", "zxcvbnmZXCVBNM"};
HashMap<Character, Integer> hashMap = new HashMap<Character, Integer>();
for(int i = 0; i < temp.length; i++){
for(int j = 0; j < temp[i].length(); j++){
hashMap.put(temp[i].charAt(j), i);
}
}
for(int i = 0; i < words.length; i++){
String test = words[i];
boolean flag = true;
int index = hashMap.get(test.charAt(0));
for(int j = 1; j < test.length(); j++){
if(hashMap.get(test.charAt(j)) != index){
flag = false;
break;
}
}
if(flag){
resultList.add(test);
}
}
String[] result = new String[resultList.size()];
for(int i = 0; i < result.length; i++){
result[i] = resultList.get(i);
}
return result;
}
0 0
- Leetcode-500. Keyboard Row
- 【Leetcode】500. Keyboard Row
- 500. Keyboard Row
- 500. Keyboard Row
- 【LeetCode】500. Keyboard Row
- 500. Keyboard Row
- LeetCode 500. Keyboard Row
- 500. Keyboard Row
- 500. Keyboard Row
- LeetCode 500. Keyboard Row
- 500. Keyboard Row
- 500. Keyboard Row (E)
- 500. Keyboard Row
- 500. Keyboard Row
- leetcode 500. Keyboard Row
- 500. Keyboard Row
- [LeetCode]500. Keyboard Row
- 500. Keyboard Row
- 一网打尽:贝佐斯与亚马逊时代
- vector的用法以及通讯录的改版
- HDU 1716 排列2 (stl,next_permutation)
- 从0到1,Python Web开发的进击之路
- SQL server服务功能
- 500. Keyboard Row
- fragment的生命周期,它与Activity生命周期的关联;
- 记事本窗口(界面)
- javascript代码规范
- EntityFramework Core并发导致显式插入主键问题
- 深入理解Java虚拟机7~晚期(运行期)优化
- 【bzoj1998】Fsk物品调度 置换群+并查集
- hello
- LINUX USB驱动开发(1)-USB驱动重要数据结构