Longest Palindromic Substring——LeetCode

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,存在唯一的最长的回文子串,求这个回文子串。

解题思路:首先这个题有O(n)的解,是在http://articles.leetcode.com/2011/11/longest-palindromic-substring-part-ii.html这里有详细的解释,本文的算法是O(n^2)的,中规中矩的做法。回文有两种,abba和aba,本文分别对子串以i为中心进行检查偶数和奇数哪个长,同时记录长度,下标,奇偶等信息。

Talk is cheap>>

  public String longestPalindrome(String s) {
        if (s == null || s.length() <= 1) {
            return s;
        }
        int max_len = 0, pos = 0;
        boolean odd = false;
        for (int i = 0; i < s.length(); i++) {
            int forward = i - 1;
            int backward = i + 1;
            int len = 0;
            while (forward >= 0 && backward < s.length() && s.charAt(forward) == s.charAt(backward)) {
                forward--;
                backward++;
                len++;
            }
            if (max_len < (len * 2 + 1)) {
                max_len = len * 2 + 1;
                odd = true;
                pos = i;
            }
            len = 0;
            forward = i;
            backward = i + 1;
            while (forward >= 0 && backward < s.length() && s.charAt(forward) == s.charAt(backward)) {
                forward--;
                backward++;
                len++;
            }
            if (max_len < len * 2) {
                max_len = len * 2;
                odd = false;
                pos = i;
            }
        }
        if (odd) {
            return s.substring(pos - (max_len - 1) / 2, pos + (max_len - 1) / 2 + 1);
        }
        return s.substring(pos - max_len / 2 + 1, pos + max_len / 2 + 1);
    }
时间: 2024-11-05 20:31:56

Longest Palindromic Substring——LeetCode的相关文章

Longest Palindromic Substring leetcode java

题目: 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(n3),会TLE. 代码如下: 1 public String longestP

Longest Palindromic Substring[leetcode]

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. class Solution { public: string longestPalindrome(string s) { int len = s.l

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 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 OJ】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. 解题思路:p

[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(3)题解: 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. 思路: 我的思路是遍

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