题解 | #最长回文子串#
C++ 解法,常规 dp
class Solution { public: int getLongestPalindrome(string A, int n) { if (A.size() <= 1) return A.size(); int l = 0, r = 0; vector<vector<bool> > dp(A.size(), vector<bool>(A.size(), false)); for (int i = 0; i < A.size(); ++i) dp[i][i] = true; for (int i = A.size() - 1; i >= 0; --i) { for (int j = i + 1; j < A.size(); ++j) { if (A[i] == A[j] && ((j - i) <= 1 || dp[i + 1][j - 1])) { dp[i][j] = true; if (j - i <= r - l) continue; r = j, l = i; } } } return r - l + 1; } };