LeetCode - 最长回文子串(No.5)

5 - 最长回文子串

  • date : Dec.30th, 2019
  • platform : windows

problem description

给定一个字符串 s,找到 s 中最长的回文子串。你可以假设 s 的最大长度为 1000。

thinking

中心扩展算法

code

class Solution {
public:
    string longestPalindrome(string s) {
        if (s.length()<= 1) return s;
        int lenMax= 0, left= 0;
        for (int i= 0; i< s.length(); i++) {
            int len1= expand(s, i, i);
            int len2= expand(s, i, i+ 1);
            int len= (len1> len2)?len1:len2;
            if (len> lenMax) {
                lenMax= len;
                left= i- (lenMax- 1)/ 2;
            }
        }
        return s.substr(left, lenMax);
    }

    int expand(string &s, int left, int right) {
        int l= left, r= right;
        while (l>= 0 && r< s.length() && s[l]== s[r]) {
            --l;
            ++r;
        }
        return r- l- 1;
    }
};

原文地址:https://www.cnblogs.com/litun/p/12122233.html

时间: 2024-08-30 16:09:57

LeetCode - 最长回文子串(No.5)的相关文章

leetcode——最长回文子串

关于这道题,我的第一想法是针对回文串的特性,对字符串的每个字符(奇数回文串)或者每两个字符(偶数回文串)向两边开始扩展分析.在这个过程中不断发现最新的最长回文串.显然这个算法的复杂度为O(n^2) class Solution  { public: string longestPalindrome(string s)  { //中心扩展法 int maxlen; int prev; int next; string result; maxlen = 0; //回文串为奇数时 for (int i

LeetCode最长回文子串

题目: 给定一个字符串 s,找到 s 中最长的回文子串.你可以假设 s 的最大长度为 1000. 示例 1: 输入: "babad"输出: "bab"注意: "aba" 也是一个有效答案. 示例 2: 输入: "cbbd"输出: "bb" 来源:力扣(LeetCode)链接:https://leetcode-cn.com/problems/longest-palindromic-substring著作权归领

LeetCode之“字符串”:最长回文子串

题目要求: 给出一个字符串(假设长度最长为1000),求出它的最长回文子串,你可以假定只有一个满足条件的最长回文串.例如,给出字符串 "abcdzdcab",它的最长回文子串为 "cdzdc". 解答: 这个题目的一个简单的解法就是对字符串中的每一个字符,同时向其两边延展,以找到最长回文子串.这种方法是可以的,但要处理回文子串长度为奇数和偶数的两种情况是比较麻烦的.如下图的几个字符串: “a” "aa" "aaa" "

【LeetCode】5# 最长回文子串

题目描述 给定一个字符串 s,找到 s 中最长的回文子串.你可以假设 s 的最大长度为 1000. 示例 1: 输入: "babad" 输出: "bab" 注意: "aba" 也是一个有效答案. 示例 2: 输入: "cbbd" 输出: "bb" 思路 本题运用了一些动态规划的思想,关于动态规划,可以看看我之前的一篇博客了解一下. LeetCode 探索初级算法 - 动态规划 1.首先要找到最简情况.这道题

[C++]LeetCode: 99 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. 思路:题目要求的s的一个最长回文子串.暴力解决办法就是枚举所有的子串,再对每个子串进行回文判断.进行剪枝,我们考虑可以使用动态规划来避免重复的判

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. 翻译: 找出字符串s中最长的回文子串,字符串s的最长是1000,假设存在唯一的最长回文子串 法一:直接暴力破解 O(N3)的时间复杂度,运行超

转载:LeetCode:5Longest Palindromic Substring 最长回文子串

本文转自:http://www.cnblogs.com/TenosDoIt/p/3675788.html 题目链接 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. 求字符串的最长回文子串 算法1:暴

[LeetCode]33. 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. 解法一:考虑回文字符串paliStr的特征,分为字符串长度为奇偶两种情况:(1)paliStr.size()为奇数时,则从最中间的一个字符往两边扩展是

[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. 最长回文子串Longest palindromic substring, 最长回文子串或最长对称因子问题是在一个字符串中查找一个最长连续子串,这个子串