LeetCode OJ 5.Longest Palindromic Substring

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 ;
                }
};



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值