500. Keyboard Row
Posted andywu
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了500. Keyboard Row相关的知识,希望对你有一定的参考价值。
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"]
题目含义:给出n个字符串,从而判断每个字符串中的字符石头来自美式键盘上的同一行,若来自同一行,返回该string
1 public String[] findWords(String[] words) { 2 List<String> okWorkd = new ArrayList<>(); 3 String row1 = "qwertyuiop"; 4 String row2 = "asdfghjkl"; 5 String row3 = "zxcvbnm"; 6 for (String word : words) { 7 if (word.isEmpty()) continue; 8 boolean row1OK = true; 9 boolean row2OK = true; 10 boolean row3OK = true; 11 for (int i=0;i<word.length();i++) 12 { 13 if (!row1.contains(String.valueOf(word.charAt(i)).toLowerCase())) 14 { 15 row1OK = false; 16 } 17 18 if (!row2.contains(String.valueOf(word.charAt(i)).toLowerCase())) 19 { 20 row2OK = false; 21 } 22 23 if (!row3.contains(String.valueOf(word.charAt(i)).toLowerCase())) 24 { 25 row3OK = false; 26 } 27 } 28 if (row1OK || row2OK || row3OK) okWorkd.add(word); 29 } 30 31 String[] reuslt = new String[okWorkd.size()]; 32 okWorkd.toArray(reuslt); 33 return reuslt; 34 }
以上是关于500. Keyboard Row的主要内容,如果未能解决你的问题,请参考以下文章