关闭

5. Longest Palindromic Substring 暴力 800ms -> dp 97ms -> 回归原始 3ms AC

标签: string
114人阅读 评论(0) 收藏 举报
分类:

Given a string s, find the longest palindromic substring in s. You may assume that the maximum length of s is 1000.

Example:

Input: "babad"

Output: "bab"

Note: "aba" is also a valid answer.

Example:

Input: "cbbd"

Output: "bb"

方法一:暴力解法,其中几个break很重要,否则会导致TLE, 800+ms AC

class Solution {
public:
    string longestPalindrome(string s) {
        int maxL = 0, rl = 0, rr = 0;
        int n = s.size();
        for(int i = 0; i < s.size(); i++){
            for(int j = n - 1; j >=i; j--){
                int left = i, right = j;
                while(left <= right){
                    if(s[left] == s[right]){
                        left++;
                        right--;
                    }else{
                        break;
                    }
                    
                }
                if(right < left){
                    if(maxL < j - i + 1){
                        maxL = j - i + 1;
                        rl = i;
                        rr = j;
                        break;
                    }
                }
                if(maxL >= n - i - 1) break;
            }
        }
        return s.substr(rl, rr - rl + 1);
    }
};
方法2:方法1的改进,用bool dp[i][j]表示 substr(i, j - i + 1)是否为回文 97ms AC

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;
        for(int i = 0; i < s.length(); i++) {
            for(int j = 0; j <= i; j++) {
                if(s.charAt(i) == s.charAt(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);
    }
}


方法3:以第i个元素左右散开(其实最先想到的就是这个方法,但是不知道那时候是脑抽了还是怎么的,认为这个方法复杂度是O(n^3),就pass了,浪费了很多时间,,想到方法1,郁闷,实际上这个的复杂度最坏的情况下也仅仅是O(n^2),也最简单)3ms AC

class Solution {
public:
    std::string longestPalindrome(std::string s) {
        if (s.size() < 2)
            return s;
        int len = s.size(), max_left = 0, max_len = 1, left, right;
        for (int start = 0; start < len && len - start > max_len / 2;) {
            left = right = start;
            while (right < len - 1 && s[right + 1] == s[right])
                ++right;
            start = right + 1;
            while (right < len - 1 && left > 0 && s[right + 1] == s[left - 1]) {
                ++right;
                --left;
            }
            if (max_len < right - left + 1) {
                max_left = left;
                max_len = right - left + 1;
            }
        }
        return s.substr(max_left, max_len);
    }
};





0
0
查看评论

LeetCode(5) Longest Palindromic Substring

如果一个字符串从左向右写和从右向左写是一样的,这样的字符串就叫做palindromic string,如aba,或者abba。给定输入一个字符串,要输出一个子串,使得子串是输入字符串的最长的padromic string。 我的思路是这样的。 以abba这样一个字符串为例来看,abba中,一共有偶数...
  • feliciafay
  • feliciafay
  • 2013-11-27 14:00
  • 17814

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, an...
  • runningtortoises
  • runningtortoises
  • 2015-05-06 15:38
  • 1349

LeetCode-5-Longest Palindromic Substring(动态规划)-Medium

题意理解: 求解最长的回文; 题目分析: 1. 通过遍历完成,但算法的复杂度过高(O(N3)); 2. 采用动态规划,复杂度为O(N2)(先前以为所谓的动态规划,就是像《算法导论》里车间的那个例子,从最终决策节点向终端节点进行递归,最终获得最优解。其实,只要是最终最优决策依赖于过程最优决策的方式...
  • eddy_liu
  • eddy_liu
  • 2015-12-20 16:49
  • 364

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 ther...
  • booirror
  • booirror
  • 2016-01-22 23:05
  • 910

Leetcode 5. Longest Palindromic Substring 新补充Manacher算法

5. Longest Palindromic Substring Total Accepted: 98755 Total Submissions: 434700 Difficulty: Medium Given a string...
  • fantasiasango
  • fantasiasango
  • 2016-03-15 09:34
  • 526

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 e...
  • sunao2002002
  • sunao2002002
  • 2015-05-12 00:38
  • 2637

[LeetCode] 005. Longest Palindromic Substring (Medium) (C++/Java/Python)

[LeetCode] 005. Longest Palindromic Substring (Medium) (C++/Java/Python)
  • hcbbt
  • hcbbt
  • 2015-02-28 00:09
  • 2914

[LeetCode]5 Longest Palindromic Substring(C++,Python实现)

LeetCode OJ的第五题,如果有问题或者给我指点欢迎来信讨论ms08.shiroh@gmail.com 题目描述
  • Shiroh_ms08
  • Shiroh_ms08
  • 2014-05-23 21:28
  • 3111

LeetCode --- 5. 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...
  • makuiyu
  • makuiyu
  • 2015-01-27 22:25
  • 797

Java Longest Palindromic Substring(最长回文字符串)

如果一个字符串从左向右写和从右向左写是一样的,这样的字符串就叫做palindromic string,如aba,或者abba。本题是这样的,给定输入一个字符串,要求输出一个子串,使得子串是最长的padromic string。 下边演示3种思路   1.两侧比较法 以abba这样一个字符串...
  • soszou
  • soszou
  • 2014-07-06 10:38
  • 16281
    个人资料
    • 访问:15304次
    • 积分:1108
    • 等级:
    • 排名:千里之外
    • 原创:99篇
    • 转载:5篇
    • 译文:0篇
    • 评论:0条