• longest common prefix


    1. Question

    找字符串数组的最长公共前缀,是所有字符串的。

    Write a function to find the longest common prefix string amongst an array of strings.

    2. Solution(O(mn))

    以第一个字符串作为前缀初值(前缀不可能比它要长,可以优化采用长度最短的字符串为初值字符串),遍历所有字符串,求当前字符串与前缀变量的公共前缀,作为新的前缀变量。

    考虑特殊情况:数组为空或数组长度为0

    import java.util.Arrays;
    import java.util.LinkedList;
    
    public class Solution {
        //O(mn)
        public String longestCommonPrefix(String[] strs){
            if( strs.length == 0 )
                return "";
            
            int len = strs[0].length();
            int k = 0;
            for( int i=1; i<strs.length; i++ )
                if( strs[i].length() < len ){
                    len = strs[i].length();
                    k = i;
                }
            String prefix = strs[k];
            for( int i=0; i<strs.length; i++){
                if( !strs[i].startsWith( prefix ) ){
                    prefix = prefix.substring(0, prefix.length()-1 );
                    i--;
                }
            }
            return prefix;
        }
    }
    View Code

    或者一个一个比较:

    import java.util.Arrays;
    import java.util.LinkedList;
    
    public class Solution {
        //O(mk)
        public String longestCommonPrefix(String[] strs){
            if( strs.length == 0 )
                return "";
    
            int k = 0;
            for( int i=1; i<strs.length; i++ )
                if( strs[i].length() < strs[k].length() )
                    k = i;
            
            int len = strs[k].length();
            int i;
            for( i=0; i<len; i++ ){
                boolean judge = true;
                for( int j=0; judge && j<strs.length; j++ )
                    judge = judge && ( strs[j].charAt(i) == strs[k].charAt(i) );
                if( judge == false )
                    break;
            }
            return strs[k].substring(0, i);
        }
    }
    View Code

    也可以用递归,即最长公共前缀是前n-1个字符串的最长公共前缀与最后一个字符的最长公共前缀:

    import java.util.Arrays;
    import java.util.LinkedList;
    
    public class Solution {
        //use iteration, O(n2)
        public String longestCommonPrefix(String[] strs){
            if( strs.length == 0 )
                return "";
            if( strs.length == 1 )
                return strs[0];
            return longestCommonPrefix( new LinkedList<String>( Arrays.asList( strs ) ) );        
        }
        
        public String longestCommonPrefix( LinkedList<String> strs ){
            if( strs.size() == 2){
                String s1 = strs.getFirst();
                String s2 = strs.getLast();
                int len = (s1.length() < s2.length()) ? s1.length() : s2.length();
                int i = 0;
                for( ; i<len && s1.charAt(i) == s2.charAt(i); i++);
                return s1.substring(0, i);
            }
            
            LinkedList<String> temp = new LinkedList<String>();
            temp.add( strs.removeLast());
            temp.add( longestCommonPrefix(strs) );
            return longestCommonPrefix( temp );
        }
    }
    View Code
  • 相关阅读:
    TCP定时器 之 重传/延迟ACK/保活 定时器初始化
    指针03-指针和字符串
    指针02
    指针01
    switch语句分析
    结构体分析
    参数、返回值、局部变量、数组分析
    多维数组分析
    循环语句分析
    if语句分析
  • 原文地址:https://www.cnblogs.com/hf-cherish/p/4596517.html
Copyright © 2020-2023  润新知