leetcode--005. Longest palindromic Substring

Source: Internet
Author: User

Longest palindromic Substring test Instructions

Give a string to find the longest palindrome substring. Solving

Each character in the string is extended to the left and right, and a palindrome substring is found.
Palindrome string length can be odd, or even, it should be noted.

Class Solution {public
:
    //88ms
    string Longestpalindrome (string s) {
        string ans;
        for (int i = 0; i < s.length (); ++i) {
            string oddstr = Getpalin (s, I, I);
            if (Oddstr.length () > Ans.length ()) ans = oddstr;

            String evenstr = Getpalin (s, I, i + 1);
            if (Evenstr.length () > Ans.length ()) ans = evenstr;
        }
        return ans;
    }

    String Getpalin (string& s, int l, int r) {while
        (l >= 0 && R < s.length () && s[l] = = S[r]) l --, r++;
        Return S.substr (L + 1, r-l-1);
    }
};

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.