题解 | #最长回文子串#
最长回文子串
https://www.nowcoder.com/practice/b4525d1d84934cf280439aeecc36f4af?tpId=295&tqId=25269&ru=/exam/company&qru=/ta/format-top101/question-ranking&sourceUrl=%2Fexam%2Fcompany
class Solution { public: /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param A string字符串 * @return int整型 */ int getLongestPalindrome(string A) { // write code here int ans = 1; int len = A.size(); vector<vector<bool>> dp(A.size(), vector<bool>(A.size(), false)); for(int i = 0; i < len; ++i) dp[i][i] = true; for(int i = len - 1; i >= 0; --i) { for(int j = 0; j < len; ++j) { if(A[i] == A[j]) { if((j - i) == 1 || (j - i) == 0) { dp[i][j] = true; ans = max(ans, j - i + 1); } if((j - i) > 1 && dp[i + 1][j - 1]) { dp[i][j] = true; ans = max(ans, j - i + 1); } } } } return ans; } };