码迷,mamicode.com
首页 > 其他好文 > 详细

Leetcode500

时间:2020-02-13 17:10:11      阅读:50      评论:0      收藏:0      [点我收藏+]

标签:etc   cte   str   bool   ring   test   map   count   class   

leetcode 500 键盘侠

这道题我的思想是,找一个相对应的映射,从而把24个字母都映射到HashMap上,从而便于自己查找。

以下便是代码:

public String[] findWords(String[] words) {

    
    int count=0;
    boolean []pos=new boolean[words.length];
    int index=0;
    for(int i=0;i<words.length;i++){
        if(test(words[i])){
            pos[i]=true;
            count++;
        }
    }
    String[]ans=new String[count];
    for(int i=0;i<pos.length;i++){
        if(pos[i]){
            ans[index++]=words[i];
        }
    }
    return ans;

}
public boolean test(String str){
    java.util.HashMap<Character,Integer>map;
    map=new java.util.HashMap<>();
    String []word={"qwertyuiopQWERTYUIOP","asdfghjklASDFGHJKL","zxcvbnmZXCVBNM"};
    for(int i=0;i<word.length;i++){
        for(int j=0;j<word[i].length();j++){
            map.put(word[i].charAt(j),i+1);
        }
    }
    if(str==null||str.length()<0)
        return false;
    int index=map.get(str.charAt(0));
    for(int i=1;i<str.length();i++){
        if(index!=map.get(str.charAt(i)))
            return false;
    }
    return true;
}

Leetcode500

标签:etc   cte   str   bool   ring   test   map   count   class   

原文地址:https://www.cnblogs.com/Yunrui-blogs/p/12303958.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!