Keyboard Row
来源:互联网 发布:apache启动和停止命令 编辑:程序博客网 时间:2024/05/17 02:26
原文地址:https://leetcode.com/problems/keyboard-row/description/
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 class KeyboardRow { private static final HashSet<Character> firstRowInKeyboard = new HashSet<Character>() { { this.add('Q'); this.add('q'); this.add('W'); this.add('w'); this.add('E'); this.add('e'); this.add('R'); this.add('r'); this.add('T'); this.add('t'); this.add('Y'); this.add('y'); this.add('U'); this.add('u'); this.add('I'); this.add('i'); this.add('O'); this.add('o'); this.add('P'); this.add('p'); } }; private static final HashSet<Character> secondRowInKeyboard = new HashSet<Character>() { { this.add('A'); this.add('a'); this.add('S'); this.add('s'); this.add('D'); this.add('d'); this.add('F'); this.add('f'); this.add('G'); this.add('g'); this.add('H'); this.add('h'); this.add('J'); this.add('j'); this.add('K'); this.add('k'); this.add('L'); this.add('l'); } }; private static final HashSet<Character> thirdRowInKeyboard = new HashSet<Character>() { { this.add('Z'); this.add('z'); this.add('X'); this.add('x'); this.add('C'); this.add('c'); this.add('V'); this.add('v'); this.add('B'); this.add('b'); this.add('N'); this.add('n'); this.add('M'); this.add('m'); } }; public static String[] findWords(String[] words) { if (words == null) return null; if (words.length == 0) return new String[]{}; List<String> resultList = new ArrayList<>(); for (int i = 0; i < words.length; i++) { if (isInSameRow(words[i])) resultList.add(words[i]); else continue; } String[] resultArray = new String[resultList.size()]; for (int i = 0; i < resultList.size(); i++) { resultArray[i] =resultList.get(i); } return resultArray; } public static boolean isInSameRow(String word) { if (word == null || word.length() == 0) return true; char firstChar = word.charAt(0); HashSet<Character> set = null; if (firstRowInKeyboard.contains(firstChar)) set = firstRowInKeyboard; else if (secondRowInKeyboard.contains(firstChar)) set = secondRowInKeyboard; else if (thirdRowInKeyboard.contains(firstChar)) set = thirdRowInKeyboard; else return false; for (int i = 0; i < word.length(); i++) { if (set.contains(word.charAt(i))) continue; else return false; } return true; } public static void main(String[] args) { String[] strArray = new String[] {"Hello", "Alaska", "Dad", "Peace"}; System.out.println(Arrays.toString(findWords(strArray))); }}
阅读全文
0 0
- Keyboard Row
- Keyboard Row
- Keyboard Row
- Keyboard Row
- Keyboard Row
- Keyboard Row
- Keyboard Row
- Keyboard Row
- Keyboard Row
- Keyboard Row
- Keyboard Row
- Leetcode-500. Keyboard Row
- 【Leetcode】500. Keyboard Row
- 500. Keyboard Row
- leetcode-500-Keyboard Row
- 500. Keyboard Row
- 【LeetCode】500. Keyboard Row
- 500. Keyboard Row
- 设计模式读书笔记-----迭代器模式
- 10.1日NOIP模拟赛
- 偏差与方差,欠拟合与过拟合
- 虚析构函数
- 设计模式
- Keyboard Row
- 分子量
- C++使用: C++中map的基本操作和用法
- 训练日记-27
- UVa133 The Dole Queue
- Windows下使用django做website(创建第一个django工程并使用pycharm打开执行)
- HDU
- SQL之存储过程
- JVM (PART VII)垃圾收集器的参数