LeetCode最长回文子串

题目:

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

示例 1:

输入: "babad"
输出: "bab"
注意: "aba" 也是一个有效答案。

示例 2:

输入: "cbbd"
输出: "bb"

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/longest-palindromic-substring
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

方法一:将字符串倒置 ,找出公共字符串子串->确定最后碰到了自己

StringBuilder strR=new StringBuilder(s).reverse();//倒置
            int len=s.length();
            int[][] arr=new int[len][len];
            int maxlen=0;
            int startIndex=0;

            for(int i=0;i<len;i++) {
                for(int j=0;j<len;j++) {
                    if(s.charAt(i)==strR.charAt(j)) {
                        if(i==0||j==0) arr[i][j]=1;//边界
                            else arr[i][j]=arr[i-1][j-1]+1;
                    }
//len-j-1+arr[i][j]-1==j    j(末尾的下标)-(len-j-1)(表示正着的开头下标)+1==arr[i][j](长度)
                    if((len-j-1+arr[i][j]-1==i)&&arr[i][j]>maxlen) {
                        maxlen=arr[i][j];
                        startIndex=len-j-1;
                    }
                }
            }
            return s.substring(startIndex,startIndex+maxlen);

方法二动态规划:将填表的过程记录下来而不是从头来

二维boolean[][]数组记录是某两位是不是回文串

选取[l,r],s[l]==s[r]之间的字符串如果他是回文串,则[l+1,r-1]也是回文串

//str[l,r] s[l]==s[r] 判断dp[l+1,r-1]是否为真
        if(s.isEmpty()) {
            return "";
        }
        int len=s.length();
        int strLen=1;
        int startIndex=0;

        boolean[][] dp=new boolean[len][len];

        for(int r=0;r<len;r++) {
            for(int l=0;l<r;l++) {
                char left=s.charAt(l);
                char right =s.charAt(r);
                if(left==right&&((r-l<=2)||dp[l+1][r-1])) {
                    dp[l][r]=true;
                    if((r-l+1)>strLen) {
                        strLen=r-l+1;
                        startIndex=l;
                    }
                }
            }
        }
        return s.substring(startIndex,startIndex+strLen);

原文地址:https://www.cnblogs.com/code-fun/p/11437651.html

时间: 2024-07-31 14:19:16

LeetCode最长回文子串的相关文章

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 - 最长回文子串(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=

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