LeetCode - 5.最长回文字串(中心扩散法)

有两种情况,第一种是中心是有数字,即最长回文的长度为奇数,一种是中心不是数字,即最长回文的长度为偶数。使用两个循环即可。

class Solution {
public:
    string longestPalindrome(string s) {
        const int n = s.size();
        if(n==0){
            return "";
        }
        
        int max_i = 0;
        int max_j = 0;
        int max_length = -1;
        
        for(int i=0;i=0 && i+j=0 && i+j

LeetCode - 5.最长回文字串(中心扩散法)_第1张图片

https://leetcode-cn.com/problems/longest-palindromic-substring/submissions/

你可能感兴趣的:(LeetCode - 5.最长回文字串(中心扩散法))