Leetcode 5 Longest palindromic Substring (C,c++,python,java)

Source: Internet
Author: User

Problem:Given A stringS, find the longest palindromic substring inS. Assume that maximum length ofSis, and there exists one unique longest palindromic substring.
Solution:find palindrome strings in the middle of each character, then record the largest palindrome string, time complexity O (n^2)Main topic:returns the longest string of strings, given a string. Problem Solving Ideas:the easiest way to think of brute force, determine each substring, and then see if it is a palindrome string, time complexity O (n^3), easy to time out, using the method in solution, time to take charge of the grams reached O (n^2), is said to be able to passManacher's Algorithmalgorithm to increase the time complexity to O (n), but not quite understand the meaning of the original link: http://blog.csdn.net/han_xiaoyang/article/details/11969497#t20Java source code (spents 294ms, it's long enough):
public class Solution {public String longestpalindrome (String s) {int max=1,maxf=0,maxe=0;            for (int i=0;i<s.length (); i++) {int end = findodd (s,i);                if (Max < (end-i) *2+1) {max = (end-i) *2+1;                MAXF = I+i-end;            Maxe=end;            } end = Findeven (s,i);                if (Max < (end-i) * *) {max = (end-i) * *;                MAXF = I+i+1-end;            Maxe = end;    }} return S.substring (maxf,maxe+1);        } public int findodd (String s,int Center) {int i=center-1,j=center+1;            while (i>=0 && j<s.length ()) {if (S.charat (i)!=s.charat (j)) return j-1;        i--;j++;    } return j-1;        } public int Findeven (String s,int Center) {int i=center,j=center+1;            while (i>=0 && j<s.length ()) {if (S.charat (i)!=s.charat (j)) return j-1;        i--;j++; } return J1; }}

C Language Source code (spents 29ms):
int findodd (char* s,int Center) {    int i=center-1,j=center+1;    while (i>=0 && s[j]) {        if (S[i]!=s[j]) return j-1;        i--;j++;    }    return j-1;} int Findeven (char* s,int Center) {    int i=center,j=center+1;    while (i>=0 && s[j]) {        if (S[i]!=s[j]) {            return j-1;        }        i--;j++;    }    return j-1;} char* Longestpalindrome (char* s) {    int i=0,end,max=1,maxf=0,maxe=0;    for (i=0;s[i];i++) {        end=findodd (s,i);        if (max< (end-i) *2+1) {            max= (end-i) *2+1;            Maxf=i+i-end; maxe=end;        }        End=findeven (s,i);        if (max< (end-i) * *) {            max= (end-i) * *;            Maxf=i+i+1-end; maxe=end;        }    }    s[maxe+1]=0;    return S+MAXF;}

C + + source code (spents 95ms):
Class Solution {Public:string Longestpalindrome (string s) {int max=1,maxf=0,maxe=0;            for (int i=0;i<s.size (); i++) {int end = findodd (s,i);                if (Max < (end-i) *2+1) {max = (end-i) *2+1;                Maxf=i+i-end;            Maxe=end;            } end = Findeven (s,i);                if (Max < (end-i) * *) {max = (end-i) * *;                Maxf=i+i+1-end;            Maxe=end;    }} return S.substr (Maxf,max);        } int findodd (string S,int Center) {int i=center-1,j=center+1;            while (i>=0 && j<s.size ()) {if (S[i]!=s[j]) return j-1;        i--;j++;    } return j-1;        } int Findeven (string S,int Center) {int i=center,j=center+1;            while (i>=0 && j<s.size ()) {if (S[i]!=s[j]) return j-1;        i--;j++;    } return j-1; }};
python source code (spents 1434ms, this really too long, to improve the algorithm):
Class solution:    # @param {string} s    # @return {string}    def longestpalindrome (self, s):        max=1; maxf=0; Maxe=0 for        i in range (0,len (s)):            end = Self.findodd (s,i)            if Max < (end-i) *2+1:                max = (end-i) *2+1                MAXF = i+i-end                maxe = end            end = Self.findeven (s,i)            if Max < (end-i) * 2:                max = (end-i) * *                MAXF = i +i+1-end                maxe = end        return s[maxf:maxe+1]    def findodd (self,s,center):        i=center-1;j=center+1 While        I>=0 and J<len (s):            if S[i]!=s[j]:return j-1            i=i-1;j=j+1        return j-1    def Findeven (self,s,center):        i=center;j=center+1 while        i>=0 and J<len (s):            if S[i]!=s[j]:return j-1            i= I-1;j=j+1        return j-1



Leetcode 5 Longest palindromic Substring (C,c++,python,java)

Related Article

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.