标签:
Given a string S, find the longest palindromic substring in S. You may assume that the maximum length of S is 1000, and there exists one unique longest palindromic substring.
class Solution { public: string longestPalindrome(string s) { if(s.length()<2) return s; int i,left,right,maxlen=0,tleft; for(i=0;i<s.length();){ if(s.length()-i<=maxlen/2) break; left=right=i; while(right<s.length()-1&&s[right+1]==s[right]) right++; i=right+1; while(left>0&&right<s.length()-1&&s[left-1]==s[right+1]){left--;right++;} if(right-left+1>maxlen){ maxlen=right-left+1; tleft=left; } } return s.substr(tleft,maxlen); } };
Leetcode 5. Longest Palindromic Substring
标签:
原文地址:http://www.cnblogs.com/Deribs4/p/5617009.html