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:
Input: "cbbd"
Output: "bb"

方法一

? 方法一关键思想,每当我们向右移动时,我们只需要考虑使用这个新字符作为尾巴是否可以产生新的回文字符串,其长度为(当前长度+1)或(当前长度+2)。 方法一优于方法二采用的动态规划。
? Java我们提供两种方法,由运行时间,我们可以看出使用char[]性能比substring()和charAt()更优。

** Solution One -- Method One **
** 6ms, 40.8MB **
class Solution {
    public String longestPalindrome(String s) {
        if (s.length() < 1) return s;
        int curLen = 0;
        String res = "";
        char[] chr = s.toCharArray();
        for (int i = 0; i < s.length(); ++i) {
            if (isPalindrome(chr, i - curLen - 1, i)) {
                res = s.substring(i - curLen - 1, i + 1);
                curLen += 2;
            } else if (isPalindrome(chr, i - curLen, i)) {
                res = s.substring(i - curLen, i + 1);
                curLen += 1;
            }
        }
        return res;
    }
    private boolean isPalindrome(char[] chr, int i, int j){
        if (i < 0 ) return false;
        while (i < j)
            if (chr[i++] != chr[j--])
                return false;
        return true;
    }
}
** Solution One -- Method Two **
** 14ms, 41.3MB **
class Solution {
    public String longestPalindrome(String s) {
        String res = "";
        int currLength = 0;
        for (int i = 0; i < s.length(); ++i) {
            if (isPalindrome (s , i - currLength - 1, i)) {
                res = s.substring(i - currLength - 1,i + 1);
                currLength = currLength + 2;
            } else if (isPalindrome(s, i - currLength, i)) {
                res = s.substring(i - currLength, i + 1);
                currLength = currLength + 1;
            }
        }
        return res;
    }
    public boolean isPalindrome(String s, int begin, int end){
        if(begin<0) return false;
        while(begin < end)
            if(s.charAt(begin++) != s.charAt(end--)) return false;
        return true;
    }
}
** Solution One **
** Python **
** 364ms, 11.8MB **
class Solution(object):
    def longestPalindrome(self, s):
        if (len(s) <= 1) :
            return s
        curLen = 0
        res = ""
        for i in range(len(s)) :
            if (self.isPalindrome(s, i - curLen - 1, i)) :
                res = s[i - curLen - 1 : i + 1]
                curLen += 2
            elif (self.isPalindrome(s, i - curLen, i)) :
                res = s[i - curLen : i + 1]
                curLen += 1
        return res
    def isPalindrome(self, s, i, j):
        if (i < 0) :
            return False
        while (i < j) :
            if (s[i] != s[j]) :
                return False
            i += 1
            j -= 1
        return True

方法二 : 动态规划

? 方法二采用动态规划,dp[j][i]表示s[j:i+1]是回文子串与否。

** Solution Two **
** 55ms,  43.4MB **
public class Solution {
    public String longestPalindrome(String s) {
        if(s == null || s.length() == 0) {
            return "";
        }
        int len = s.length();
        boolean[][] dp = new boolean[len][len];
        int start = 0;
        int end = 0;
        int max = 0;
        char[] chr = s.toCharArray();
        for (int i = 0; i < s.length(); ++i) {
            for (int j = 0; j <= i; ++j) {
                if (chr[i] == chr[j] && (i - j <= 2 || dp[j+1][i-1])) {
                    dp[j][i] = true;
                }
                if (dp[j][i] && max < i - j + 1) {
                    max = i - j + 1;
                    start = j;
                    end = i;
                }
            }
        }
        return s.substring(start, end + 1);
    }
}

原文地址:https://www.cnblogs.com/willwuss/p/12257873.html

时间: 2024-10-08 01:19:29

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

LeetCode 5 Longest Palindromic Substring(最大回文子字符串)

翻译 给定一个字符串S,找出它的最大回文子字符串. 你可以假定S的最大长度为1000, 并且这里存在唯一一个最大回文子字符串. 原文 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. 暴力搜索,O(n

LeetCode-5:Longest Palindromic Substring(最长回文子字符串)

描述:给一个字符串s,查找它的最长的回文子串.s的长度不超过1000. Input: "babad" Output: "bab" Note: "aba" is also a valid answer. 我是采用动态规划解决此题的.官方的solutions中提供了几种思路,包括我使用的DP.这里摘要如下: 思路1: 将s反转得到s',然后查找s和s'的最长公共子串substring,那么substring就是最长回文子串.比如:s = "

[C++]LeetCode: 99 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的一个最长回文子串.暴力解决办法就是枚举所有的子串,再对每个子串进行回文判断.进行剪枝,我们考虑可以使用动态规划来避免重复的判

[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. 最长回文子串Longest palindromic substring, 最长回文子串或最长对称因子问题是在一个字符串中查找一个最长连续子串,这个子串

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

[LeetCode]33. 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. 解法一:考虑回文字符串paliStr的特征,分为字符串长度为奇偶两种情况:(1)paliStr.size()为奇数时,则从最中间的一个字符往两边扩展是

Leetcode 5. Longest Palindromic Substring(最长回文子串, Manacher算法)

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: Input: "cbbd"

LeetCode 5 Longest Palindromic Substring 最长回文子序列 manacher算法 string.substr 难度:2

https://leetcode.com/problems/longest-palindromic-substring/ manacher算法:http://blog.csdn.net/ywhorizen/article/details/6629268 string longestPalindrome(string s) { char ch[2001];int p[2001]; ch[2*s.size()] = 0; for(int i = 0; i < 2 * s.size(); i++) {

[Leetcode] 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. 做这道题之前要先了解什么是回文子串.回文串通俗的解释是,分别从字符串两端开始遍历,得到的结果相同,如"abba",从两端的遍历结果都是:&q