【LeetCode】Keyboard Row 解题报告
2017-04-02 19:16
525 查看
【LeetCode】Keyboard Row 解题报告
标签(空格分隔): LeetCode题目地址: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 :
Input: [“Hello”, “Alaska”, “Dad”, “Peace”]Output: [“Alaska”, “Dad”]
Ways
暴力解决了。分别把三行弄在三个数组里,对于每个单词每个字母都去循环,数在三行中的个数分别多少。如果这个单词能在一张中打出来完,那么说明由某一行的个数为1,其他行都为0.注意字符串数组的写法。
public class Solution { public String[] findWords(String[] words) { char []arr1 = new char[]{'q','w','e','r','t','y','u','i','o','p','Q','W','E','R','T','Y','U','I','O','P'}; char []arr2 = new char[]{'a','s','d','f','g','h','j','k','l','A','S','D','F','G','H','J','K','L'}; char []arr3 = new char[]{'z','x','c','v','b','n','m','Z','X','C','V','B','N','M'}; List<String> ans = new ArrayList<String>(); for(String word: words){ int count1 = 0, count2 = 0, count3 = 0; for(int i =0; i < word.length(); i++){ for(int j =0; j < arr1.length; j++){ if(word.charAt(i) == arr1[j]){ count1++; } } for(int j =0; j < arr2.length; j++){ if(word.charAt(i) == arr2[j]){ count2++; } } for(int j =0; j < arr3.length; j++){ if(word.charAt(i) == arr3[j]){ count3++; } } } if((count1 != 0 && count2 == 0 && count3 == 0) ||(count1 == 0 && count2 != 0 && count3 == 0) ||(count1 == 0 && count2 == 0 && count3 != 0)){ ans.add(word); } } String []answer = new String[ans.size()]; for(int i =0; i < ans.size(); i ++){ answer[i] = ans.get(i); } return answer; } }
Date
2017 年 4 月 2 日相关文章推荐
- [Leetcode] 656. Coin Path 解题报告
- [leetcode]50. Pow(x, n)@Java解题报告
- Leetcode 435 python 解题报告
- [Leetcode] 204. Count Primes 解题报告
- [Leetcode] 664. Strange Printer 解题报告
- [Leetcode] 775. Global and Local Inversions 解题报告
- [Leetcode] 669. Trim a Binary Search Tree 解题报告
- LeetCode - 268. Missing Number - stable_sort应用实例 - ( C++ ) - 解题报告
- [Leetcode] 414. Third Maximum Number 解题报告
- LeetCode: Largest Number 解题报告 以及Comparator, CompareTo 应用
- [leetcode] 156. Binary Tree Upside Down 解题报告
- leetcode解题报告21. Merge Two Sorted Lists
- [LeetCode] 423. Reconstruct Original Digits from English 解题报告
- LeetCode 46. Permutations 解题报告
- [Leetcode] 517. Super Washing Machines 解题报告
- [Leetcode] 138. Copy List with Random Pointer 解题报告
- 【LeetCode】Basic Calculator 解题报告
- LeetCode84 Largest Rectangle in Histogram 解题报告
- Leetcode 650. 2 Keys Keyboard 2指键盘 解题报告
- [Leetcode] 687. Longest Univalue Path 解题报告