LeetCode-014-最长公共前缀

最长公共前缀

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

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

示例说明请见LeetCode官网。

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/longest-common-prefix/
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

解法一:字符数组法

把第一个字符串放到字符数组chars中,从第二个字符串开始,挨个字符遍历比较,得到每次不相等的索引位置p,如果p-1小于0,则表示没有公共前缀;遍历后面的字符串,重复这一过程。 中间只要p-1小于0,直接返回空字符串,没有公共前缀。 遍历结束后,获取chars数组中从0到p-1位置的字符,返回即为最长公共前缀。

public class Solution {
    public static String longestCommonPrefix(String[] strs) {
        if (strs == null || strs.length == 0 || strs[0] == null || strs[0].length() == 0) {
            return "";
        }
        char[] chars = strs[0].toCharArray();
        int maxIndex = strs[0].length() - 1;
        for (int i = 1; i < strs.length && maxIndex >= 0; i++) {
            String curStr = strs[i];
            if (curStr == null || curStr.length() == 0) {
                return "";
            }
            int curMax = maxIndex;
            for (int j = 0; j < curStr.length() && j <= curMax && maxIndex >= 0; j++) {
                if (curStr.charAt(j) != chars[j]) {
                    maxIndex = j - 1;
                    break;
                } else {
                    maxIndex = j;
                }
            }
        }
        if (maxIndex < 0) {
            return "";
        }
        StringBuilder result = new StringBuilder();
        for (int i = 0; i <= maxIndex; i++) {
            result.append(chars[i]);
        }
        return result.toString();
    }

    public static void main(String[] args) {
        System.out.println(longestCommonPrefix(new String[]{"flower", "flow", "flight"}));
        System.out.println(longestCommonPrefix(new String[]{"ab", "a"}));
    }
}
posted @   醉舞经阁  阅读(25)  评论(0编辑  收藏  举报
编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· DeepSeek 开源周回顾「GitHub 热点速览」
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
点击右上角即可分享
微信分享提示