2020-08-25 力扣题五

2020-08-25  本文已影响0人  Joyner2018

最长回文数

class Solution {
public:
    string longestPalindrome(string s) {
        if (s == "")return "";
        int len = s.length();
        int index = 0,maxL=0,begin=0;
        while (index < len) {
            int right = index, left = index;
            while (index < len&&s[index + 1] == s[index]) {
                index++;
                right++;
            }
            while (right < len&&left >= 0 && s[right] == s[left]) {
                right++;
                left--;
            }
            right--, left++;
            if (right-left+ 1 > maxL) {
            maxL = right - left + 1;
            begin = left;
            }
            index++;
        }
        return s.substr(begin, maxL);
    }
};
上一篇 下一篇

猜你喜欢

热点阅读