每日一道算法题LeetCode14:Longest Common Prefix(最长公共前缀)

题目

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

如果不存在公共前缀,返回空字符串 “”。

题目链接: link.

分析

题目并不难,函数输入为一个字符串数组,输出为一个字符串,要求这个字符串是最长的公共前缀,题目的解法多样,有横向比较,纵向比较(最容易想到),还有分治法,二分法。

题解

纵向比较(V1)

这个解法比较容易想到,先来展示第一个版本:

    public static String longestCommonPrefix(String[] strs) {
        if(strs == null || strs.length==0)  return "";  
        int minLen = Integer.MAX_VALUE;   // 找到最短字符串长度
        for(String s:strs){
            if(s.length()<minLen)   minLen = s.length();
        }
        for(int i=0 ; i<minLen ; i++){
            char temp = strs[0].charAt(i);
            for(String s:strs){
                if(s.charAt(i) != temp) return strs[0].substring(0,i);  // 有一个不同,直接返回子串作为结果
            }
        }
        return strs[0].substring(0,minLen);  // 上面的循环结束,没有返回,说明最短字串就是最长前缀
    }

纵向比较(V2)

写完上面之后,再后来看题解是发现,其实并不需要找到最短的长度,于是有了第二个版本,其实这两个版本效率区别并不大,但是代码简洁一些:

    public static String longestCommonPrefix(String[] strs) {
        if(strs == null || strs.length==0)  return "";

        int length = strs[0].length();
        for (int i = 0; i < length; i++) {
            char temp = strs[0].charAt(i);
            for(int j=1 ; j<strs.length ; j++){
            	// 如果j字符串长度就等于i,直接返回
                if(i == strs[j].length() || strs[j].charAt(i) != temp)  return strs[0].substring(0,i);
            }
        }
        return strs[0];  // 上面的循环结束,没有返回,说明strs[0]就是最长前缀
    }

分治法

分治法比较难想到,但是想到后就会觉的很简单,官方题解代码如下:

public static String longestCommonPrefix(String[] strs) {
        if (strs == null || strs.length == 0) {
            return "";
        } else {
            return longestCommonPrefix(strs, 0, strs.length - 1);
        }
    }
    
    public static String longestCommonPrefix(String[] strs,int start,int end){
        if (start == end) {
            return strs[start];
        } else {
            int mid = (end - start) / 2 + start;
            String lcpLeft = longestCommonPrefix(strs, start, mid);
            String lcpRight = longestCommonPrefix(strs, mid + 1, end);
            return commonPrefix(lcpLeft, lcpRight);
        }
    }
    
    public static String commonPrefix(String lcpLeft, String lcpRight) {
        int minLength = Math.min(lcpLeft.length(), lcpRight.length());
        for (int i = 0; i < minLength; i++) {
            if (lcpLeft.charAt(i) != lcpRight.charAt(i)) {
                return lcpLeft.substring(0, i);
            }
        }
        return lcpLeft.substring(0, minLength);
    }

二分法

二分法先找到最短的字符串,然后使用二分查找找到最长前缀,官方题解代码如下:

public static String longestCommonPrefix(String[] strs) {
        if (strs == null || strs.length == 0) {
            return "";
        }
        int minLength = Integer.MAX_VALUE;
        for (String str : strs) {
            minLength = Math.min(minLength, str.length());
        }
        int low = 0, high = minLength;
        while (low < high) {
            int mid = (high - low + 1) / 2 + low;
            if (isCommonPrefix(strs, mid)) {
                low = mid;
            } else {
                high = mid - 1;
            }
        }
        return strs[0].substring(0, low);
    }

    public static boolean isCommonPrefix(String[] strs, int length) {
        String str0 = strs[0].substring(0, length);
        int count = strs.length;
        for (int i = 1; i < count; i++) {
            String str = strs[i];
            for (int j = 0; j < length; j++) {
                if (str0.charAt(j) != str.charAt(j)) {
                    return false;
                }
            }
        }
        return true;
    }

总结

题目不难,纵向比较应该是很好想到的,使用分治和二分挺有趣的,可以自己敲一下!

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值