1. 程式人生 > >Leetcode 500.鍵盤行

Leetcode 500.鍵盤行

ins ima int arraylist bre adf i++ 打印 ++

鍵盤行

給定一個單詞列表,只返回可以使用在鍵盤同一行的字母打印出來的單詞。鍵盤如下圖所示。

技術分享圖片

示例:

輸入: ["Hello", "Alaska", "Dad", "Peace"]

輸出: ["Alaska", "Dad"]

註意:

  1. 你可以重復使用鍵盤上同一字符。
  2. 你可以假設輸入的字符串將只包含字母。

 1 import java.util.ArrayList;
 2 import java.util.List;
 3 
 4 class Solution {
 5     public String[] findWords(String[] words) {
6 List<String> res=new ArrayList<String>(); 7 String row1="wqertyuiopQWERTYUIOP"; 8 String row2="sadfghjklASDFGHJKL"; 9 String row3="zxcvbnmZXCVBNM"; 10 for(String word:words){ 11 int a=0; 12 int b=0; 13 int c=0;
14 for(int i=0;i<word.length();i++){ 15 if(row1.contains(word.charAt(i)+"")) a=1; 16 else if(row2.contains(word.charAt(i)+"")) b=1; 17 else if(row3.contains(word.charAt(i)+"")) c=1; 18 if(a+b+c>1) break; 19 }
20 if(a+b+c==1) res.add(word); 21 } 22 String[] str=new String[res.size()]; 23 for(int i=0;i<str.length;i++){ 24 str[i]=res.get(i); 25 } 26 return str; 27 } 28 }

Leetcode 500.鍵盤行