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);

Solution:

public class Solution {
    private static int MAX_LENGH = 1000;
    public String longestPalindrome(String s) {
        char[] ch = s.toCharArray();
        int len = s.length();
        int maxLen = 1;
        int longestBegin = 0;
        boolean[][] table = new boolean[len+1][len+1];
        // Arrays.fill(table,false);

        for(int i = 0; i < len; i++){
            table[i][i] = true;
        }
        for(int i = 0; i < len-1; i++){
            if(ch[i] == ch[i+1]){
                table[i][i+1] = true;
                longestBegin = i;
                maxLen = 2;
            }
        }
        for(int l = 3 ; l <= len; l++){
            for(int i = 0; i< len - l+1; i++){
                int j = i + l -1;
                if(ch[i] == ch[j] && table[i+1][j-1]){
                    table[i][j] = true;
                    maxLen = l;
                    longestBegin = i;
                }
            }
        }
        return s.substring(longestBegin,longestBegin + maxLen);
    }
}

O(n) 线性时间复杂度算法

极其酷炫掉渣天的方法

http://leetcode.com/2011/11/longest-palindromic-substring-part-ii.html

LeetCode:Longest Palindromic Substring,布布扣,bubuko.com

时间: 2024-12-18 17:31:52

LeetCode:Longest Palindromic Substring的相关文章

LeetCode: Longest Palindromic Substring(Medium)

原题链接:https://leetcode.com/problems/longest-palindromic-substring/description/ 1. 题目要求:找出字符串中的最大回文子串 2. 注意:要考虑回文子串中的字符个数是奇数还是偶数!!! 例如,"aabaa"是一个奇数个字符的回文字符串,他的中心只有一个字符"b". "aabbaa"是一个偶数个字符的回文字符串,他的中心却有两个相同字符"bb" 3. 思路

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

LeetCode5: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. 解题思路: 主要有三种: 第一种:Manacher算法,也是最快的,时间复杂度为O(n) 第二种:DP算法,时间复杂度为O(n*n) 第三种:

LeetCode OJ: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. 玩了两天dota2,罪过罪过,还是应该老老实实刷题啊. 题目求得是最长的回文子串,这里使用的是暴力的解法,也就是解决两种回文"asdsa"以

LeetCode第[5]题(Java):Longest Palindromic Substring 标签:String、动态规划

题目中文:求最长回文子串 题目难度:Medium 题目内容: Given a string s, find the longest palindromic substring in s. You may assume that the maximum length of s is 1000. 翻译: 给定一个字符串s,找出s中最长的回文子串.你可以假设s的最大长度是1000. 什么叫回文子串? 就是字符串中,满足能正读反读都一样的子串,就是回文子串.如下所示 Input: "babad"

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

[LeetCode][Python]Longest Palindromic Substring

# -*- coding: utf8 -*-'''__author__ = '[email protected]'https://oj.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 exis