5. Longest Palindromic Substring

class Solution {
    bool IsPalindrome(string s, int low, int high) {
        while(low <= high) {
            if(s[low] != s[high]) return false;
            low++;
            high--;
        }
        return true;
    }
public:
    string longestPalindrome(string s) {
        int low = 0;
        int maxlength = 0;
        int length = 0;
        int maxlow = 0, maxhigh = 0;
        while(low + maxlength < s.size()) { // or while(s.size() - 1 - low + 1 > maxlength)
            for(int i = low + maxlength; i < s.size(); i++) {//maybe faster if direction is from right to left
                if(s[low] == s[i]) {
                    if(IsPalindrome(s, low, i)) {
                        length = i -low + 1;
                        if(length > maxlength) {
                            maxlength = length;
                            maxlow = low;
                            maxhigh = i;
                        }
                    }
                }
            }
            low++;
        }
        return s.substr(maxlow, maxlength);
    }
};

很简单的想法,

从左到右遍历, one by one, O(n**2)

-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

O(n)

from:http://blog.csdn.net/feliciafay/article/details/16984031

    // Transform S into T.  
    // For example, S = "abba", T = "^#a#b#b#a#$".  
    // ^ and $ signs are sentinels appended to each end to avoid bounds checking  
    string preProcess(string s) {  
      int n = s.length();  
      if (n == 0) return "^$";  
      string ret = "^";  
      for (int i = 0; i < n; i++)  
        ret += "#" + s.substr(i, 1);  
       
      ret += "#$";  
      return ret;  
    }  
       
    string longestPalindrome(string s) {  
      string T = preProcess(s);  
      int n = T.length();  
      int *P = new int[n];  
      int C = 0, R = 0;  
      for (int i = 1; i < n-1; i++) {  
        int i_mirror = 2*C-i; // equals to i' = C - (i-C)  
          
        P[i] = (R > i) ? min(R-i, P[i_mirror]) : 0;  
          
        // Attempt to expand palindrome centered at i  
        while (T[i + 1 + P[i]] == T[i - 1 - P[i]])  
          P[i]++;  
       
        // If palindrome centered at i expand past R,  
        // adjust center based on expanded palindrome.  
        if (i + P[i] > R) {  
          C = i;  
          R = i + P[i];  
        }  
      }  
       
      // Find the maximum element in P.  
      int maxLen = 0;  
      int centerIndex = 0;  
      for (int i = 1; i < n-1; i++) {  
        if (P[i] > maxLen) {  
          maxLen = P[i];  
          centerIndex = i;  
        }  
      }  
      delete[] P;  
        
      return s.substr((centerIndex - 1 - maxLen)/2, maxLen);  
    }  


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值