LeetCode#5 Longest Palindromic Substring (week3)

week3

题目

Given a string s, find the longest palindromic substring in s. You may assume that the maximum length of s is 1000.

Example:
Input: “babad”
Output: “bab”
Note: “aba” is also a valid answer.

Example:
Input: “cbbd”
Output: “bb”

解析

题目要求对于一个给定的字符串s,找出其最长的回文串。解题思路大致为用一个字符串记录当前找到的最大的回文串,以s的每个字符作为回文串的中轴,向两边扩展得到以该字符作为中轴的最长字符串,并与记录的最长字符串比较长度决定是否替换。
在本题具体解题过程中我将以单独一个字符作为中轴及以两个字符作为中轴独立为Odd及Even两个类中的一个方法。

代码

/*检索一个字符串s中以两个字符作为中轴的最长回文串*/
class Even {
public:
    string longestEvenString(string s) {
        string result = "";
        for (int i = 0; i < s.size(); ++i)
        {
            bool doWhile = false;
            int j = i + 1;
            int k = i;
            if (j < s.size())
            {
                while (s[k] == s[j])
                {
                    doWhile = true;  /*进入while循环说明可以找到中轴*/
                    if ((k - 1 < 0) || (j + 1 > s.size() - 1)) /*检索已到达字符串头部或尾部,停止检索*/
                    {
                        k--;
                        j++;
                        break;
                    }
                    else
                    {
                        k--;
                        j++;
                    }
                }
            }
            string temp = "";
            if (doWhile)
            {
                k++;
                j--;
                temp = s.substr(k, j - k + 1);
            }
            else
            {
                /*没有进入while循环,说明在此位置不存在以两个相同字符作为中轴的回文串*/
                temp = "";
            }
            if (temp.size() > result.size()) /*得到新的最长的回文串*/
            {
                result = temp;
            }
        }
        return result;
    }
};
/*检索一个字符串s中以单独一个字符作为中轴的最长回文串*/
class Odd {
public:
    string longestOddString(string s) {
        string result = "";
        for (int i = 0; i < s.size(); ++i)
        {
            /*j记录当前检索的位置的中轴偏移量*/
            int j = 0;
            while ((i - j >= 0) && ((i + j) <= (s.size() - 1))) /*检索已到达字符串头部或尾部,停止检索*/
            {
                if (s[i - j] == s[i + j])
                {
                    j++;
                }
                else {
                    break;
                }
            }
            j--;
            string temp = s.substr(i - j, 2 * j + 1);
            if (temp.size() > result.size()) /*得到新的最长的回文串*/
            {
                result = temp;
            }
        }
        return result;
    }
};
class Solution {
public:
    string longestPalindrome(string s) {
        Odd O;
        Even E;
        string temp1 = O.longestOddString(s);
        string temp2 = E.longestEvenString(s);
        if (temp1.size() > temp2.size())
        {
            return temp1;
        }
        else
        {
            return temp2;
        }
    }
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值