前言:为了后续的实习面试,开始疯狂刷题,非常欢迎志同道合的朋友一起交流。因为时间比较紧张,目前的规划是先过一遍,写出能想到的最优算法,第二遍再考虑最优或者较优的方法。如有错误欢迎指正。博主首发,mcf171专栏。
博客链接:mcf171的博客
——————————————————————————————
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 Solution { public String[] findWords(String[] words) { char[] row1 = new char[]{'q','w','e','r','t','y','u','i','o','p'}; char[] row2 = new char[]{'a','s','d','f','g','h','j','k','l'}; char[] row3 = new char[]{'z','x','c','v','b','n','m'}; List<char[]> rows = new ArrayList<char[]>(); rows.add(row1); rows.add(row2); rows.add(row3); List<String> results = new ArrayList<String>(); for(String item : words){ for(char[] row : rows){ int flag = check(item,row); if(flag == 1) {results.add(item);break;} else if(flag == -1) break; } } return (String[]) (results.toArray(new String[results.size()])); } private int check(String str, char[] chs){ str = str.toLowerCase(); int flag = 0; boolean exits = false; boolean not_exits = false; for(int i = 0; i < str.length(); i++){ char ch = str.charAt(i); if(contains(ch,chs)) exits = true; else not_exits = true; if(exits && not_exits){flag = -1;break;} } if(!exits) return 0; if(exits && !not_exits) return 1; if(flag == -1) return -1; return flag; } private boolean contains(char ch, char[] chs){ boolean exits = false; for(char item : chs){ if(ch == item) { exits = true; break;} } return exits; } }
