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 Solution {
    public String longestPalindrome(String s) {
        int m=s.length();
        String ans=null;
        if(m==0)
        return s;
        if(m==1)
        return s;

        for(int length=m;length>=0;length--){
            for(int i=0;i+length-1<m;i++){
                ans=s.substring(i,i+length-1);
                if(isPalindromic(ans))
                return ans;
            }

        }
        return ans;

    }

     public static boolean isPalindromic(String l){
            int k=l.length();
            for(int i=0;i<k/2;i++){
                if(l.charAt(i)!=l.charAt(k-1-i))
                return false;
            }
            return true;
        }
}

思路二:动态规划。dp[i][j]=true when dp[i+1][j-1]=true&&s.charAt(i)==s.charAt(j);

public class Solution {
    public String longestPalindrome(String s) {
       int n = s.length();
       int start = 0;
       int maxLength = 1;
       boolean dp[][] = new boolean[n+1][n+1];
  for (int i = 0; i < n; i++) {
    dp[i][i] = true;
  }
  for (int i = 0; i < n-1; i++) {
    if (s.charAt(i) == s.charAt(i+1)) {
      dp[i][i+1] = true;
      start = i;
      maxLength = 2;
    }
  }
  for (int len = 3; len <= n; len++) {
    for (int i = 0; i < n-len+1; i++) {
      int j = i+len-1;
      if (s.charAt(i) == s.charAt(j)&& dp[i+1][j-1]) {
        dp[i][j] = true;
        start = i;
        maxLength = len;
      }
    }
  }
  return s.substring(start, start+maxLength);  

    }

}

思路三:http://articles.leetcode.com/2011/11/longest-palindromic-substring-part-ii.html这个方法效率最高,现在的水平还想不出这样的方法。

时间: 2024-08-06 03:44:35

LeetCode 5:Longest Palindromic Substring(最长回文串)的相关文章

[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]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 (最长回文子字符串)(动态规划)

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] 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. http://fisherlei.blogspot.com/2012/12/leetcode-longest-palindromic-substrin

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

【翻译】Longest Palindromic Substring 最长回文子串

原文地址: http://www.cnblogs.com/zhxshseu/p/4947609.html%20 转载请注明出处:http://www.cnblogs.com/zhxshseu/p/4947609.html 问题描述:Given a string S, find the longest palindromic substring in S. 这道题目是一个经典的动态规划DP http://challenge.greplin.com/问题,在面试中经常会被问到.为什么?因为这个问题可