zoukankan      html  css  js  c++  java
  • LeetCode: Longest Palindromic Substring

    Title : Longest Palindromic Substring

    Given a string S, find the longest palindromic substring in S. You may assume that the maximum length of S is 1000, and there exists one unique longest palindromic substring.

    思路 1:扩展

    每个字母单独都是一个长度为1的回文串,因此对每个字母我们可以向外扩展,不过需要考虑"abba","abcba"的情况,就是长度为奇数还是偶数。时间复杂度就是O(n2)

    string getSubstring(string s,int i,int j){
    		while (i >= 0 && j < s.size() && s[i] == s[j]){
    			i--;
    			j++;
    		}
    		return s.substr(i+1,j-i-1);
    	}
    	
    	string longestPalindrome(string s){
    		if (s.size() == 0)
    			return "";
    		string ret;
    		int max = 0;
    		for (int i = 0 ; i < s.size(); i++){
    			string result = getSubstring(s,i,i);
    			if (result.size() > max){
    				max = result.size();
    				ret = result;
    			}
    			result = getSubstring(s,i,i+1);
    			if (result.size() > max){
    				max = result.size();
    				ret = result;
    			}
    		}
    		return ret;
    	}
    

    思路二:使用动态规划。A[i,j]表示i和j是否相等。与其它的动态规划不同的是,这边,我们以长度进行扩展。分别以长度为2,3,...扩大判断

    string longestPalindrome(string s){
            int n = s.length();
            int maxLength = 1;
            int left = 0;
            bool ** table = new bool*[n];
            for (int i = 0 ; i < n; i++){
                table[i] = new bool[n];
            }
            for (int i = 0 ; i < n; i++){
                for (int j = 0; j < n; j++){
                    if (i == j)
                        table[i][j] = true;
                    else
                        table[i][j] = false;
                }
            }
            for (int len = 2; len <= n; len++){
                for (int i = 0 ; i < n; i++){
                    int j = i+len-1;
                    if (j >= n)
                        break;
                    if (s[i] != s[j]){
                        continue;
                    }else{
                        if (i+1 > j-1)
                            table[i][j] = true;
                        else{
                            table[i][j] = table[i+1][j-1];
                        }
                    }
                    if (table[i][j] && (len > maxLength)){
                        maxLength = len;
                        left = i;
                        cout<<i<<" "<<j<<" "<<maxLength<<" "<<s.substr(left,maxLength)<<endl;
                    }
                }
            }
            
            return s.substr(left,maxLength);
    
        }

    思路3:将字符串反转,得到两个字符串,通过求解最长公共字串

    string longestPalindrome(string s){
            string s1 = s;
            reverse(s.begin(),s.end());
            return LCS(s1,s);
        }
        string LCS(string s1,string s2){
            int n = s1.size();
            int m = s2.size();
            int maxLength = 0;
            int left = 0;
            int** table = new int* [n+1];
            for (int i = 0 ; i <= n; i++){
                table[i] = new int[m+1];
            }
            for (int i = 0 ; i <= n; i++)
                table[i][0] = 0;
            for (int j = 0 ; j <= m; j++)
                table[0][j] = 0;
            for (int i = 0 ; i < n; i++){
                for (int j = 0 ; j < m; j++){
                    if (s1[i] == s2[j]){
                        table[i+1][j+1] = table[i][j]+1;
                    }else{
                        table[i+1][j+1] = 0;
                    }
                    if (maxLength < table[i+1][j+1]){
                        maxLength = table[i+1][j+1];
                        left = i;
                    }
                }
            }
            return s1.substr(left-maxLength+1,maxLength);
        }
  • 相关阅读:
    23 抽象类 abstract
    22.4 Extends --super 和 this 的区别
    22.3 Extends 构造方法的执行顺序
    22.2 继承的 成员变量的执行顺序
    22.1 Extends 继承的 方法重写@Override 和 方法重载Overload 的区别
    22 Extends 继承(子类、父类)
    21.3代码块考试题
    vue-cli3安装
    ==隐式转换是怎样进行的?
    为什么有时候人们用translate来改变位置而不是定位?
  • 原文地址:https://www.cnblogs.com/yxzfscg/p/4417042.html
Copyright © 2011-2022 走看看