标签:highlight longest pre ges substr string stp solution sub
class Solution { public: string longestPalindrome(string s) { int len=s.size(); int left,right; int begin=0,maxlen=0; if(len<=1) return s; else { for(int i=1;i<len;i++) { left=i-1; right=i; while(left>=0&&right<len&&s[left]==s[right]) { left--; right++; } if(right-left-1>maxlen) { begin=left+1; maxlen=right-left-1; } left=i-1; right=i+1; while(left>=0&&right<len&&s[left]==s[right]) { left--; right++; } if(right-left-1>maxlen) { begin=left+1; maxlen=right-left-1; } } } return s.substr(begin,maxlen); } };
标签:highlight longest pre ges substr string stp solution sub
原文地址:http://www.cnblogs.com/xlqtlhx/p/7896106.html