题解 | #最长回文子串#
最长回文子串
https://www.nowcoder.com/practice/b4525d1d84934cf280439aeecc36f4af
#include <string> class Solution { public: /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param A string字符串 * @return int整型 */ int getLongestPalindrome(string A) { // write code here // 找回文子串的长度 // 从头开始找和该值相等,开始回头检查是否是回文字串 int Asize = A.size(); vector<int> value(Asize+1, 0); int maxLen = 0; if(Asize<2){ return Asize; } for(int i = 0; i<Asize; i++){ int len = 0; for(int j = Asize-1; j>0; j--){ cout<<i<<","<<j<<A[i]<<A[j]<<endl; if(A[i] == A[j]){ // 发生相等,检查里面是否是回文 if(j-i+1>2){ string tmp = A.substr(i+1,j-i-1); cout<<tmp<<endl; bool isOrder = isList(tmp); if(isOrder){ len = j-i+1; cout<<i<<","<<j<<","<<j-i+1<<endl; break; } else{ continue; } } else{ len = j-i+1; break; } } } cout<<"len"<<len<<endl; // 更新maxlen if(maxLen<len){ maxLen = len; } } return maxLen; } bool isList(string s){ // 检查该内容是否是回文 int Bsize = s.size(); if(s[0] == s[Bsize-1]){ if(Bsize > 2){ return isList(s.substr(1,Bsize-2)); } return true; } else{ return false; } } };