键盘行 字符串处理 力扣 500

我这个代码尽量没使用库函数,所以有点长;

首先三个数组来存储键盘的三行字母;

然后遍历我们的words String数组;

每次拿一个单词先转换小写英文字母,再来判断是否它的所有字母都出现在键盘期中一行;

如果出现在期中一行那么,把它存到ArrayList ,为什么ArrayList?因为如果存储普通String数组时没办法返回完全空格;

所以需要ArrayList来存储他们;

判断的时候;

我们再遍历一个单词的每一个字母是否在这个对应的一行;

注意每一个一行的要单独判断否子会出现,一个词的某个字母在一行某个字母在另外一行;

class Solution {

    private char[] first = {'q','w','e','r','t','y','u','i','o','p'};
    private char[]second = {'a','s','d','f','g','h','j','k','l'};
    private char[]third = {'z','x','c','v','b','n','m'};

    public String[] findWords(String[] words) {
        
        ArrayList<String>res = new ArrayList<>();
        int j = 0;
     for(int i = 0;i<words.length;i++){
        if(isInFirst(words[i].toLowerCase()))
            res.add(words[i]);
         if(isInSecond(words[i].toLowerCase()))
            res.add(words[i]);
         if(isInThird(words[i].toLowerCase()))
            res.add(words[i]);
     }

    String []fin = res.toArray(new String[res.size()]);

           return fin;
    }

    public boolean isInFirst(String word){
         for(int i = 0;i<word.length();i++){
            if(!checkFir(word.charAt(i)))
                return false;
         }
         return true;
    }
    public boolean checkFir(char ch){
        for(int i = 0;i<first.length;i++){
            if(first[i]==ch)
              return true;
        }
        return false;
    }
    public boolean isInSecond(String word){
       for(int i = 0;i<word.length();i++){
            if(!checkSec(word.charAt(i)))
                return false;
         }
         return true;
    }
    public boolean checkSec(char ch){
        for(int i = 0;i<second.length;i++){
            if(second[i]==ch)
              return true;
        }
        return false;
    }
    public boolean isInThird(String word){
         for(int i = 0;i<word.length();i++){
            if(!checkThir(word.charAt(i)))
                return false;
         }
         return true;
    }

    public boolean checkThir(char ch){
        for(int i = 0;i<third.length;i++){
            if(third[i]==ch)
              return true;
        }
        return false;
    }

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值