题解 | #最长回文子串#
最长回文子串
https://www.nowcoder.com/practice/b4525d1d84934cf280439aeecc36f4af
//记录一下我的绞尽脑汁 #include <cmath> class Solution { public: /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param A string字符串 * @return int整型 */ bool isPalindrome(string s){ int size = s.size(); int pos = int(size/2), start; if(size%2==0) start = pos; else start = pos+1; string front = s.substr(0, pos), back = s.substr(start, pos); string tmp(back.rbegin(), back.rend()); if(front == tmp) return true; return false; } int getLongestPalindrome(string A) { // write code here map<string, int> storage; int size = A.size(); if(size == 1) return 1; else if(size==2){ if(A[0] == A[1]) return 2; else return 1; } for(int i = 0;i<size;i++){ for(int pos = 2;pos<=size&&i+pos<=size;pos++){ string tmp = A.substr(i, pos); storage[tmp]++; } } vector<string> res; for(auto it:storage) if(isPalindrome(it.first)) res.push_back(it.first); int mx = res[0].size(); for(int i = 1;i<res.size();i++) mx = (mx>=res[i].size())?mx:res[i].size(); return mx; } };