LeetCode - 5 - Longest Palindromic Substring

题目

URL:https://leetcode.com/problems/longest-palindromic-substring

解法

一、循环搜索

对于每一个字符,往后搜索,遇到相同字符,开始判断是否回文串,若是回文串则与当前最长回文串的长度比较,若更长,则更新最长回文串。

显然是三层循环:第一层确定回文串起始位置,第二层确定回文串终止位置,第三层判断是否是回文串。

代码也比较简洁,可是循环太多。

    public String longestPalindrome(String s) {
        String maxString = null;
        int maxLength = 0;
        for (int i = 0, left = i; i < s.length(); i++) {
            for (int j = i, right = j; j < s.length(); j++, left = i, right = j) {
                while (left < s.length() && right >= 0 && left < right && s.charAt(left) == s.charAt(right)) {
                    left++;
                    right--;
                }
                if (left >= right && j - i + 1 > maxLength) {
                    maxLength = j - i + 1;
                    maxString = s.substring(i, j + 1);
                }
            }
        }
        return maxString;
    }

循环搜索,时间复杂度O(n3),运行时间约为 TLE。

二、向外查找

对于每一个字符,假定它(回文串长度为奇数)或者它和它的右面字符(回文串长度为偶数)是回文串,那么我们只需要向外扩张,直到它不是回文串。

该算法其奥妙在于假定和向外扩张,而不是盲目搜索。

    private int start;
    private int maxLength;

    public String longestPalindrome(String s) {
        if (s.length() == 0) return "";
        for (int i = 0; i < s.length(); i++) {
            findPalindrome(s, i, i);
            findPalindrome(s, i, i + 1);
        }
        return s.substring(start, start + maxLength);
    }

    private void findPalindrome(String s, int left, int right) {
        while (left >= 0 && right < s.length() && s.charAt(left) == s.charAt(right)) {
            left--;
            right++;
        }
        if (right - left - 1 > maxLength) {
            start = left + 1;
            maxLength = right - left - 1;
        }
    }

向外查找,时间复杂度O(n2),运行时间约为 17 ms。

总结

有的时候思路要放开,不要太计算机思维(搜索),有时也要像现实一样思考。

时间: 2024-10-12 21:44:39

LeetCode - 5 - Longest Palindromic Substring的相关文章

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

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

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][JavaScript]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. https://leetcode.com/problems/longest-palindr

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

Java for LeetCode 005 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. 解题思路一: 暴力枚举 共N^2量级个子串(从下标零开始),每次检查需一个for循环,等于是3重for循环,时间复杂度O(n^3) 解题思路二: 动态

【LeetCode】Longest Palindromic Substring 解题报告

DP.KMP什么的都太高大上了,自己想了个朴素的遍历方法. [题目] 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(medium难度)

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" Ou