标签:i+1 longest ram long substring 回文 div size inpu
class Solution { public: /** * @param s input string * @return the longest palindromic substring */ string longestPalindrome(string &s) { // Write your code here using namespace std; const int length=s.size(); if(length==0) { return NULL; } if(length==1) { return s; } int max=0; int start; for(int i=0;i<length;i++) { int j=i-1,k=i+1; while(j>=0&&k<length&&s.at(j)==s.at(k)) { if(k-j+1>max) { max=k-j+1; start=j; } j--; k++; } } for(int i=0;i<length;i++) { int j=i,k=i+1; while(j>=0&&k<length&&s.at(j)==s.at(k)) { if(k-j+1>max) { max=k-j+1; start=j; } j--; k++; } } if(max>0) { return s.substr(start,max); } return NULL; } };
标签:i+1 longest ram long substring 回文 div size inpu
原文地址:http://www.cnblogs.com/ZJJ644758437/p/6536560.html