当前位置:   article > 正文

java实现:查找字符串中最长回文子串 ---- leetCode notes_java 输入一个字符串,求出其中最长的回文子串。子串的含义是:在原串中连续出现的

java 输入一个字符串,求出其中最长的回文子串。子串的含义是:在原串中连续出现的

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;
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65

这个时间复杂度最坏情况可以达到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;
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69

这里是采用二分的一种思想实现,时间复杂度大概nlogn,所以比之前种好很多.

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/秋刀鱼在做梦/article/detail/804610
推荐阅读
相关标签
  

闽ICP备14008679号