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.
寻找最长回文串
有更长的回文串的话,那么比它短 2 的回文串必定存在。
分开搜两种情况,奇数和偶数。都找到后,返回最大值。
先搜偶数的,从2开始,,然后搜奇数的话,从偶数最大长度+1开始搜寻。未搜寻到就返回,有就搜索。
搜索回文串时,从之前的最长回文串旁,开始进行搜索,如果+2时直接取得新的回文串长度,就直接几下,下次继续搜索+2.否则,依旧搜寻本长度,直到找到新的该长度回文串,再次进行本算法。
最开始写的几个由于时间过长都没通过
bool palindromicstring (string s)
{
int iLen = s.length();
int iEnd = iLen- 1;
if (iLen == 1)
{
return true ;
}
if (iLen == 2 )
{
if (s [1] != s[ 0])
{
return false ;
}
else
{
return true ;
}
}
for (int i = 0;i <=(1+ iLen)/2;i ++)
{
if (s [i] != s[ iEnd - i ])
{
return false ;
}
}
return true ;
}
class Solution {
public:
string longestPalindrome (string s)
{
int iMaxLen = 0;
int iBegin = 0;
int iEnd = 0;
int iLen = s.length();
string strPalindrome ;
string strPali ;
for (int j = iLen; j != 0; j--)
{
for (int i = 0;i < iLen;i ++)
{
if (i +j >iLen)
{
break;
}
else
{
strPali = s.substr( i,j );
if (palindromicstring (strPali) && strPalindrome.length () < strPali.length ())
{
strPalindrome= strPali;
}
}
}
}
return strPalindrome ;
}
};
优化了下,依旧没过
改成不进行截断直接判断更快些
bool palindromicstring (const string& s,int iBegin, int iLen )
{
int iEnd = iLen + iBegin - 1;
if (iLen == 1)
{
return true ;
}
if (iLen == 2 )
{
if (s [iBegin] != s[ iBegin+1] )
{
return false ;
}
else
{
return true ;
}
}
for (int i = 0;i <=(1+ iLen)/2;i ++)
{
if (s [iBegin+ i] != s[ iEnd - i ])
{
return false ;
}
if ((iEnd - i)<= ( iBegin + i ))
{
return true ;
}
}
return true ;
}
class Solution {
public:
string longestPalindrome (string s)
{
int iMaxLen = 0;
int iBegin = 0;
int iEnd = 0;
int iLen = s.length();
string strPalindrome ="";
string strPali ;
for (int j = iLen; j != 0; j--)
{
for (int i = 0;i < iLen;i ++)
{
if (i +j >iLen)
{
break;
}
else
{
if (palindromicstring (s, i, j) && strPalindrome.length () <j)
{
strPalindrome= s.substr( i,j );
break;
}
}
}
if (strPalindrome !="")
{
break;
}
}
return strPalindrome ;
}
};
最后使用自增长,求回文。过了
最终版本 66%
采用自增长的模式,获取一个位置,以该位置为中心的最长回文串。有两种 aba abba。分别以一个字符、两个字符为中心
string PalindromicstringTwo (const string& s,int iBegin)
{
int i = 0;
int iSign = iBegin;
int iLen = s.length();
while (iSign >= 0 && (iBegin+1 + i) < iLen )
{
if (s [iSign] == s[ iBegin+1 + i ])
{
iSign--;
i++;
}
else
{
break;
}
}
iSign++;
i--;
return s .substr( iSign, 2 * (iBegin - iSign+1));
}
string PalindromicstringOne (const string& s, int iBegin)
{
int i = 0;
int iSign = iBegin;
int iLen = s.length();
while (iSign >=0 &&(iBegin+ i)< iLen )
{
if (s [iSign] == s[ iBegin + i ])
{
iSign--;
i++;
}
else
{
break;
}
}
iSign++;
i--;
return s .substr( iSign, 2 * (iBegin - iSign) + 1);
}
class Solution {
public:
string longestPalindrome (string s)
{
int iMaxLen = 0;
int iBegin = 0;
int iEnd = 0;
int iLen = s.length();
string strPalindrome ="";
string strPali ;
for (int i = 0;i < iLen;i ++)
{
strPali = PalindromicstringOne( s, i );
if (strPalindrome .length() < strPali.length ())
{
strPalindrome = strPali;
}
strPali = PalindromicstringTwo( s, i );
if (strPalindrome .length() < strPali.length ())
{
strPalindrome = strPali;
}
}
return strPalindrome ;
}
};