Advertisement

LC最长公共前缀

阅读量:

题目

在这里插入图片描述

方法一:纵向比较

对字符串数组中的每个字符串进行逐位对比,当发现首个不一致的字符位置时,即为最长公共前缀的终止点。

复制代码
    public String longestCommonPrefix(String[] strs) {
        if(strs.length == 0)
            return "";
        int minLen = Integer.MAX_VALUE;
        for(String str : strs){
            minLen = Math.min(minLen , str.length());
        }
        for(int i = 0; i < minLen; i++){
            char c = strs[0].charAt(i);
            for(String str : strs){
                if(c != str.charAt(i))

全部评论 (0)

还没有任何评论哟~