1. 程式人生 > >java實現:查詢字串中最長迴文子串 ---- leetCode notes

java實現:查詢字串中最長迴文子串 ---- leetCode notes

Given a string s, find the longest palindromic substring in s. You may assume that the maximum length of s is 1000.
提供一個字串s,找出其最長的迴文子串

所謂的迴文子串就是正序反序均一直的字串,當然也可以這樣理解:

選定字串s,以其中軸為中心線,然後左右比較,在有限範圍內,中心像兩邊同時遍歷,元素均相等

  • 1.首先提供兩種程式設計演算法:
   public String getPailStr(String s){
        int n = s.length();
        String str = ""
; int maxNums = 0; List<Character> list = new ArrayList(); List<Character> keyList = new ArrayList(); int j = 0; if(n == 1){ return s; } while(j < n){ //n if(!list.contains(s.charAt(j))){ list
.add(s.charAt(j++));//c--j:0; }else{ int length = 0; list.add(s.charAt(j));//continue next cc -- j=1; for(int i = 0;i < list.size();i++){ //m if(list.get(i) == s.charAt(j)){//i:0 keyList.clear(); length = list
.size() - i; if(length> maxNums){ keyList.clear(); for(int m = i;m < list.size();m++){// 0 2 ++ keyList.add(list.get(m)); //cc } if(revers(keyList))b{//2 //p maxNums = keyList.size(); str = ""; for(char c:keyList){ str = str + c; } break; } } } } j++; } } return str; } public boolean revers(List list){ int middle = list.size()/2; int i = 0; int j = 0; if(list.size() % 2 == 0){ i = middle -1; j = middle; }else if(list.size() % 2 == 1){ i = middle - 1; j = middle + 1; } while(i >= 0 && j <= list.size() -1){ if(list.get(i--) == list.get(j++)){ continue; }else{ return false; } } return true; }

這個時間複雜度最壞情況可以達到n*n*n,所以好汚疑問是一個比較差的實現
ok,那麼優化下一種:

   public String getPailStr(String s){
        int n = s.length();
        String str = "";
        int maxNums = 0;
        List<Character> list = new ArrayList();
        List<Character> keyList = new ArrayList();
        int j = 0;
        if(n == 1){
            return s;
        }
        while(j < n){
            if(!list.contains(s.charAt(j))){
                list.add(s.charAt(j++));//c--j:0;
            }else{
                int length = 0;
                list.add(s.charAt(j));//continue  next  cc --  j=1;
                for(int i = 0;i < list.size();i++){
                    if(list.get(i) == s.charAt(j)){//i:0
                        keyList.clear();
                        length = list.size() - i;
                        if(length> maxNums){
                            keyList.clear();
                            for(int m = i;m < list.size();m++){// 0 2 ++
                                keyList.add(list.get(m)); //cc
                            }
                            if(revers(keyList))b{//2
                                maxNums = keyList.size();
                                str = "";
                                for(char c:keyList){
                                        str = str + c;
                                }

                                break;
                            }
                        }
                    }
                }
                j++;

            }  
        }

        // for(char c:tarList){
        //      str = str + c;
        // }
        return str;
    }   

    public boolean revers(List list){
        int middle = list.size()/2;
        int i = 0;
        int j = 0;
        if(list.size() % 2 == 0){
            i = middle -1;
            j = middle;
        }else if(list.size() % 2 == 1){
            i = middle - 1;
            j = middle + 1;
        }

        while(i >= 0 && j <= list.size() -1){
            if(list.get(i--) == list.get(j++)){
                continue;
            }else{
                return false;
            }
        }
        return true;
    }

這裡是採用二分的一種思想實現,時間複雜度大概nlogn,所以比之前種好很多.