Longest Palindromic Substring (最长回文串)

2014-11-23 22:37:25 ? 作者: ? 浏览: 8
题目:
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.
题意寻找并输出字符串中的最长回文串。
没想到什么特别好的算法,就上暴力加些剪枝。
枚举回文串的长度,从最长的开始,寻找是否有当前长度的回文串。
如果找到,直接弹出循环,没找到就减小回文串的长度。
l记录满足条件的回文串在原串中的下标,r记录回文串的长度。
class Solution {  
public:  
    string longestPalindrome(string s) {  
        int i,j,k,len=s.length(),l=0,r=1;  
        for(k=len;k>=2;--k)  
        {  
            for(i=0;i+k<=len;++i)          
            {  
                for(j=0;j 
  

-->

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: