Leetcode 5. 最长回文子串(Longest Palindromic Substring)

方法一:

class Solution {
public:
    string longestPalindrome(string s) 
    {
        if (s.size() < 2) return s;
        int n = s.size(), maxLen = 0, start = 0;
        for (int i = 0; i < n - 1; ++i) 
        {
            searchPalindrome(s, i, i, start, maxLen);
            searchPalindrome(s, i, i + 1, start, maxLen);
        }
        return s.substr(start, maxLen);
    }
    void searchPalindrome(string s, int left, int right, int& start, int& maxLen) 
    {
        while (left >= 0 && right < s.size() && s[left] == s[right]) 
        {
            --left; 
            ++right;
        }
        if (maxLen < right - left - 1) 
        {
            start = left + 1;
            maxLen = right - left - 1;
        }
    }
};

 

上一篇:Leetcode 14 - Longest Common Prefix


下一篇:最大公共子序列和最大公共子串