LeetCode 5 Longest Palindromic Substring(C,C++,Python,Java)

Problem:

Given a string S,
find the longest palindromic substring in S.
You may assume that the maximum length of S is
1000, and there exists one unique longest palindromic substring.

Solution:

以每一个字符为中间查找回文串,然后记录下最大的回文串,时间复杂度O(n^2)

题目大意:

给定一个字符串,求字符串的最长回文字串。

解题思路:

最容易想到暴力破解的方法,确定每一个子串,然后查看是否是回文串,时间复杂度O(n^3),容易超时,采用Solution中的方法,时间负责度克达到O(n^2),据说可以通过Manacher’s Algorithm算法来将时间复杂度提升到O(n),但是还没太明白意思,原文链接:http://blog.csdn.net/han_xiaoyang/article/details/11969497#t20

Java源代码(用时294ms,真是够长的):

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)*2){
                Max = (end-i)*2;
                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 j-1;
    }
}

C语言源代码(用时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)*2){
            Max=(end-i)*2;
            Maxf=i+i+1-end;Maxe=end;
        }
    }
    s[Maxe+1]=0;
    return s+Maxf;
}

C++源代码(用时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)*2){
                Max = (end-i)*2;
                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源代码(用时1434ms,这个真心太长了,要改进算法):

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)*2
                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

时间: 2024-10-11 21:01:30

LeetCode 5 Longest Palindromic Substring(C,C++,Python,Java)的相关文章

LeetCode #5 Longest Palindromic Substring (M)

[Problem] Given a string S, find the longest palindromic substring in S. You may assume that the maximum length of S is 1000, and there exists one unique longest palindromic substring. [Analysis] 这题的思路有很多种,网上也有各种讨论.这里我采用的是个人觉得比较好理解的一种利用Dynamic Progra

LeetCode(4) || Longest Palindromic Substring 与 Manacher 线性算法

LeetCode(4) || Longest Palindromic Substring 与 Manacher 线性算法 题记 本文是LeetCode题库的第五题,没想到做这些题的速度会这么慢,工作之余全部耗在这上面了,只怪自己基础差.本文主要介绍使用Manacher线性算法来求解字符串的最长回文子字符串. 题目 Given a string S, find the longest palindromic substring in S. You may assume that the maxim

LeetCode:Longest Palindromic Substring

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, and there exists one unique longest palindromic substring. 动态规划解法 T(n) = O(n^2)  ,S(n) = O(n^2); Solutio

Leetcode 5 Longest Palindromic Substring (最长回文子字符串)(动态规划)

Leetcode 5 题目描述 Given a string s, find the longest palindromic substring in s. You may assume that the maximum length of s is 1000. 例子 Example 1: Input: "babad" Output: "bab" Note: "aba" is also a valid answer. Example 2: Inp

LeetCode 5 Longest Palindromic Substring(最长子序列)

题目来源:https://leetcode.com/problems/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, and there exists one unique longest palindromic substring. 动态规划,类似于l

leetcode 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, and there exists one unique longest palindromic substring. 翻译: 找出字符串s中最长的回文子串,字符串s的最长是1000,假设存在唯一的最长回文子串 法一:直接暴力破解 O(N3)的时间复杂度,运行超

[LeetCode][JavaScript]Longest Palindromic Substring

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, and there exists one unique longest palindromic substring. https://leetcode.com/problems/longest-palindr

LeetCode 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, and there exists one unique longest palindromic substring. 思路一:(超时)简单的截取长度为length(length递减)的字串,判断其是否为回文串.第一个满足要求的回文串最长. public class S

Java for LeetCode 005 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, and there exists one unique longest palindromic substring. 解题思路一: 暴力枚举 共N^2量级个子串(从下标零开始),每次检查需一个for循环,等于是3重for循环,时间复杂度O(n^3) 解题思路二: 动态