Leetcode oj:longest palindromic Substring (longest back text string)

Source: Internet
Author: User

Given a string S, find the longest palindromic substring in s. The maximum length of S is assume, and there exists one unique longest palindromic substring.

Play for two days dota2, sin sin, or should honestly brush the question ah.

The problem is the longest palindrome string, which is used in the solution of violence, that is, to solve the two palindrome "Asdsa" and "Assa", that is, odd and even palindrome, equivalent to using a double cycle, the code is as follows:

1 classSolution {2  Public:3     stringLongestpalindrome (strings) {4         if(!s.size ())return "";5         stringret;6         stringtmp;7          for(inti =0; I < s.size (); ++i) {8TMP = Findpal (s, I-1, i +1);9             if(Tmp.size () >ret.size ())TenRET =tmp; OneTMP = Findpal (s, I, i +1); A             if(Tmp.size () >ret.size ()) -RET =tmp; -         } the         returnret; -     } -      -     stringFindpal (string& S,intLeftintRight ) +     { -         if(Left <0) +             returnS.substr (left +1,1); A         if(Right >=s.size ()) at             returnS.substr (Right-1,1); -  -          while(Left >=0&& Right <s.size ()) { -             if(S[left]! =S[right]) -                  Break; -left--; inright++; -         } toleft++; +         returnS.substr (left, right-Left ); -     } the};

Leetcode oj:longest palindromic Substring (longest back text string)

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.