首页 > 其他 > 详细

Leetcode14

时间:2020-02-13 17:32:38      阅读:44      评论:0      收藏:0      [点我收藏+]

Leetcode 14

要求,编写一个函数来查找字符串组中的最长公共前缀

使用了Java语言编写,主要是利用函数indexOf和substring,从而进行比较和剪切。

public String longestCommonPrefix(String[] strs) {
        if (strs == null || strs.length < 1)
            return "";
        String a=strs[0];
        for(int i=1;i<strs.length;i++){
            while(strs[i].indexOf(a)!=0){//进行比较
                a=a.substring(0,a.length()-1);//裁剪字符串
                if(a.isEmpty()){//如果裁剪的字符串是空串
                    return "";
                }
            }
        }
        return a;
    }

Leetcode14

原文:https://www.cnblogs.com/Yunrui-blogs/p/12303990.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!