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.

Example:

Input: "babad"

Output: "bab"

Note: "aba" is also a valid answer.

Example:

Input: "cbbd"

Output: "bb"

class Solution {
public:
    string longestPalindrome(string s) {
        int startIdx = 0, left = 0, right = 0, len = 0;
        for (int i = 0; i < s.size() - 1; ++i) {
            if (s[i] == s[i + 1]) {
                left = i;
                right = i + 1;
                searchPalindrome(s, left, right, startIdx, len);
            }
            left = right = i;
            searchPalindrome(s, left, right, startIdx, len);
        }
        if (len == 0) len = s.size();
        return s.substr(startIdx, len);
    }
    void searchPalindrome(string s, int left, int right, int &startIdx, int &len) {
        int step = 1;
        while ((left - step) >= 0 && (right + step) < s.size()) {
            if (s[left - step] != s[right + step]) break;
            ++step;
        }
        int wide = right - left + 2 * step - 1;
        if (len < wide) {
            len = wide;
            startIdx = left - step + 1;
        }
    }
};
思路:

      遍历s中的元素(直到i<s.size()-1),以s[i]或者s[i]和s[i+1]为中心向两边搜索回文串。考虑到有“bob”和“noon”这两种情况,当s[i]=s[i+1]的时候,不仅要以s[i]和s[i+1]为中心向两边搜索,还要以s[i]为中心向两边搜索,比如“ccaaacc”这种情况,当s[3]=s[4]=a时,最后最长的回文串并不是以s[3]和s[4]为中心的,而是以s[3]为中心;当s[i]!=s[i+1]的时候,那就直接以s[i]为中心向两边搜索即可。至于“搜索”这个操作,我们单独再写一个函数searchPalindrome完成,该函数更新startIdx和len的长度,最后通过string的substr函数(从startIdx下标开始,len为长度拷贝字符串),返回一个最大回文串。

    关于searchPalindrome函数的实现,初始化向两边搜索的步长值为1,在搜索的过程中,如果左右两边相等,则步长+1,继续循环检测;如果左右两边不相等,则跳出循环,将上一次左右两边相等的参数(right、left、step的值)拿出来求得左右两边相等时回文串的长度wide,并跟len比较,len最后得到的是最长的wide值(因为题目要求是最长回文串),因次有一个if语句来进行len值和startIdx的更新,如果len不是最长的了(len<wide),则将len和startIdx值更新,否则不变,具体注释如下:

class Solution {
public:
	string longestPalindrome(string s) {
	//startIdx是最后返回字符串的启示下标,len是返回字符串的长度,left是开始搜索的左边起始下标,right是开始搜索的右边起始下标
	int startIdx = 0, left = 0, right = 0, len = 0;
	//这里i<s.size()-1而不是i<s.size()的原因:下面有一句if (len == 0) len = s.size();为了防止“a"这种情况出现,若i<s.size()-1,则”a"就不进入for循环,len和startIdx的值不更新,都是0,这样符合if语句的len==0,起始位置为0,长度为size,即1,返回一个a;如果i<s.size(),则要进入循环,s[i+1]就会出现
	for (int i = 0; i < s.size() - 1; ++i) {
	/*首先判断s[i]和s[i+1]是否相等。若相等,不仅要以s[i]和s[i+1]为中心向两边检测,还要以s[i]本身为中心向两边检测;如果不相等,只需
	以s[i]为中心向两边检测
	*/
	      if (s[i] == s[i + 1]) {
		left = i;
		right = i + 1;
		searchPalindrome(s, left, right, startIdx, len);
			}
	     left = right = i;
	     searchPalindrome(s, left, right, startIdx, len);
		}
	     if (len == 0) len = s.size();//防止“a"这种情况出现
	     return s.substr(startIdx, len);
	}
	void searchPalindrome(string s, int left, int right, int &startIdx, int &len) {
	       int step = 1;//搜索步长
	       while ((left - step) >= 0 && (right + step) < s.size()) {
		if (s[left - step] != s[right + step]) break;
		++step;
		}
	      int wide = right - left + 2 * step - 1;//wide=right-left+1+2*(step-1)= right - left + 2 * step - 1
	      if (len < wide) {
		len = wide;
		startIdx = left - step + 1;//具体试个例子即可
		}
	}
};

参考博客:http://www.cnblogs.com/grandyang/p/4464476.html






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值