[LeetCode]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.


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.

public class Solution {
    public String[] findWords(String[] words) {
        ArrayList<String> res = new ArrayList<String>();
        String row0="QqWwEeRrTtYyUuIiOoPp";
        String row1="AaSsDdFfGgHhJjKkLl";
        String row2="ZzXxCcVvBbNnMm";
        boolean[] whichr={false,false,false};
        int count;
        
        for(int i=0; i<words.length;i++){
           
            count=0;
            for(int z=0;z< whichr.length;z++){
                whichr[z]=false;
            }
            
            for(int j=0;j<words[i].length();j++){
                if(row0.indexOf(words[i].substring(j,j+1)) != -1) whichr[0]=true;
                if(row1.indexOf(words[i].substring(j,j+1)) != -1) whichr[1]=true;
                if(row2.indexOf(words[i].substring(j,j+1)) != -1) whichr[2]=true;
            }
            for(boolean ww : whichr){
                if(ww) count++;
            }
            
            if(count==1)
                res.add(words[i]);
        }
        
        String[] ares = new String[res.size()];
        for(int i=0;i<res.size();i++){
            ares[i]=res.get(i);
        }
        
        for(String str : ares)
			System.out.println(str);
        
        return ares;
    }
}

一些坑:

1、对数组,用arr.length

2、对String,用str.length()

3、下面这个赋值操作:

for(int z=0;z< whichr.length;z++){
                whichr[z]=false;
            }
一开始我是用这样:

for(boolean ww2 : whichr){
            	ww2=false;
            }
这样是不对的,which里面的值没有被更新。

4、ArrayList的toArray

String[] array =new String[list.size()];
list.toArray(array);

5、获取String里面的单独的char的方法

char[] strBit = word.toCharArray(); //变成char的数组
char a1=word.charAt(j);  



另一种方法,用map

public static String[] findWords2(String[] words) {
        String[] Str = {"QWERTYUIOP","ASDFGHJKL","ZXCVBNM"};
        Map<Character,Integer> map = new HashMap<>();
        for(int i=0; i<Str.length; i++) {
            for(char c : Str[i].toCharArray()) {
                map.put(c, i);
            }
        }
        int index = 0;
        List<String> res = new ArrayList<>();
        for(String word : words) {
            if (word.equals("")) continue;
            index = map.get(word.toUpperCase().toCharArray()[0]);
            for(char c : word.toUpperCase().toCharArray()) {
                if(map.get(c) != index) {
                    index = -1;//不用设置flag 直接把index设为-1即可
                    break;
                }
            }
            if(index != -1) res.add(word);
        }
        return res.toArray(new String[res.size()]);
    }



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值