leetcode500: Keyboard Row

来源:互联网 发布:网络电视机那个牌子好 编辑:程序博客网 时间:2024/05/16 09:17

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.


American keyboard


Example 1:

Input: ["Hello", "Alaska", "Dad", "Peace"]Output: ["Alaska", "Dad"]

Note:

  1. You may use one character in the keyboard more than once.
  2. You may assume the input string will only contain letters of alphabet.

注意:这个程序说明如何返回一个String[] 

public String[] findWords(String[] words) {String q1 = "QWERTYUIOP";String a1 = "ASDFGHJKL";String z1 = "ZXCVBNM";ArrayList q = new ArrayList();ArrayList a = new ArrayList();ArrayList z = new ArrayList();List<String> re = new LinkedList<>();for (int i = 0; i < q1.length(); i++)q.add(q1.charAt(i));for (int i = 0; i < a1.length(); i++)a.add(a1.charAt(i));for (int i = 0; i < z1.length(); i++)z.add(z1.charAt(i));for (int i = 0; i < words.length; i++) {char[] candidate = words[i].toUpperCase().toCharArray();boolean flag1 = true;boolean flag2 = true;boolean flag3 = true;for (int j = 0; j < candidate.length; j++) {if (q.contains(candidate[j]))flag1 = false;else if (a.contains(candidate[j]))flag2 = false;else if (z.contains(candidate[j]))flag3 = false;elsebreak;}if (((flag1 == false) && (flag2 == true) && (flag3 == true))|| ((flag1 == true) && (flag2 == false) && (flag3 == true))|| ((flag1 == true) && (flag2 == true) && (flag3 == false)))re.add(words[i]);}return re.toArray(new String[0]);}

0 0