关闭

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

标签: string
87人阅读 评论(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

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:11560次
    • 积分:1057
    • 等级:
    • 排名:千里之外
    • 原创:99篇
    • 转载:5篇
    • 译文:0篇
    • 评论:0条