【每日一题】Day0001:力扣题库NO.748. 最短补全词

前言:

         数日前开始学习JAVA基础,发现了宝藏网站力扣,沉迷做题。做了几日感觉部分题目还是值得记录下来的,于是便随即动起手来。

        如前所说,自己刚开始学习JAVA,有一定的C语言基础,基础语法和一定程度的编程能力是没问题的,也愿意思考和解决问题,但是对于算法和优化是完全小白。所以目前阶段以尽可能独立自主完成题目为第一目标。

        那么废话不多少,上题(连接如下)。

​​​​​​​力扣icon-default.png?t=LA92https://leetcode-cn.com/problems/shortest-completing-word/

第一步整理和分析下题目:

1.给定的字符串licensePlate由数字、大小写字母或空格 ' ' 组成,而对比的word[i]里的字符串都仅由小写字母组成。所以licensePlate需要过滤处理一下

2.需要反复判断一个字符串是否被另外一个字符串全包含,因此提取为一个单独方法

3.记录每次成功被包含情况下的长度和下标,最后返回最短者。

代码如下:

package cn.daycode.leetcode;

public class ShorttestCW {
    public static void main(String[] args) {
        String licensePlate = "iMSlpe4";
        String[] words = {"claim","consumer","student","camera","public","never","wonder","simple","thought","use"
};

        System.out.println(shortestCompletingWord(licensePlate,words));

    }

    public static String shortestCompletingWord(String licensePlate, String[] words) {
        int pos = 0; //记录下标
        int size = -1; //记录最短长度
        // 获得去掉空格,大写改小写后的字符串
        String removedLicensePlate = removeNoneLowerChar(licensePlate);

        /* 
         遍历word数组,分别与removedLicensePlate比较,
         如果前者包含后者,且该成员长度比之前记录的长度更短,记录下标和长度
         遍历结束后返回记录的word[pos](题目说一定会存在,因此不需要考虑不存在的情况)
        */
        for(int i=0; i<words.length; i++){
            if(isContened(removedLicensePlate, words[i])){
                if(size == -1 || words[i].length() < size) {
                    size = words[i].length();
                    pos = i;
                }
            }
        }

        return words[pos];
    }

    // 判断removedLicensePlate是否被word全包含
    public static boolean isContened(String removedLicensePlate, String word) {
        // 如果被比较的word长度比removedLicensePlate还短,那一定不能被包含
        if(removedLicensePlate.length() > word.length()){
            return false;
        }
        // 因为要用到deleteCharAt,将word转变为一个StringBuffer对象
        StringBuffer tWord = new StringBuffer(word);

        /* 
         检索removedLicensePlate,逐个字符去tWord里查找并删除
         直到字符遍历完,或者tWord已经被删完
         如果没能删除(flag == -1),返回false
         如果没能检索removedLicensePlate,返回false
         其他情况返回true
        */
        int i = 0; // 最后需要判断一下是否检索removedLicensePlate到头,所以在for外面声明i
        for(;i<removedLicensePlate.length() && tWord.length()>0;i++){
            int flag = tWord.indexOf(""+removedLicensePlate.charAt(i));
            if(flag >= 0 ){
                tWord.deleteCharAt(flag);
            }else{
                return false;
            }
        }
        if(i<removedLicensePlate.length() && tWord.length()>0){
            return false;
        }else{
            return true;
        }
    }

    // 去掉一个字符串的非英文字母,并将大写字母变小写返回
    public static String removeNoneLowerChar(String str){
        StringBuffer temp = new StringBuffer();
        // 将大写字母转换成小写,非小写字母直接去除,构成新的SB temp并返回
        for(int i=0,j=0; i < str.length(); i++){
            if(str.charAt(i) >= 'A' && str.charAt(i) <= 'Z'){
                temp.append((char)(str.charAt(i)-('A'-'a')));
            }else if(str.charAt(i) >= 'a' && str.charAt(i) <= 'z'){
                temp.append(str.charAt(i));
            }
        }

        return temp.toString();
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值