首页 > 其他分享 >LeetCode 5_最长回文子串

LeetCode 5_最长回文子串

时间:2023-01-05 12:12:32浏览次数:44  
标签:子串 int maxlen 字符串 LeetCode dp 回文

LeetCode 5:最长回文子串

题目

给你一个字符串 s,找到 s 中最长的回文子串。
如果字符串的反序与原始字符串相同,则该字符串称为回文字符串。

示例 1:
输入:s = "babad"
输出:"bab"
解释:"aba" 同样是符合题意的答案。

示例 2:
输入:s = "cbbd"
输出:"bb"

思路

使用动态规划,首先确定dp含义,dp[i][j]表示区间[i,j]之间的子串是否为回文子串。接下来确定递推公式,回文子串从外到内比较,判断i与j位置字母一致后,若dp[i+1][j-1]为true则可以赋值dp[i][j],又因为i+1,j-1可能造成i>j的现象,所以需单独判断。初始化,默认dp值为false。确定遍历顺序,因为根据dp[i+1][j-1]推断dp[i][j],所以应该从下到上,从左到右遍历。

代码

class Solution {
    public String longestPalindrome(String s) {
        int maxlen=0;
        int left=0;
        int right=0;
        boolean [][]dp=new boolean [s.length()][s.length()];//默认为false
        
        for(int i=s.length()-1;i>=0;i--)
        {
            for(int j=i;j<s.length();j++)
            {
                if(s.charAt(i)==s.charAt(j))
                {
                    if(j-i<=1)//i+1,j-1可能造成i>j
                        dp[i][j]=true;
                    else if(dp[i+1][j-1])
                        dp[i][j]=true;
                    
                }
                
                if(dp[i][j]&&j-i+1>maxlen)//保存最大回文子串左右位置
                {
                    maxlen=j-i+1;
                    left=i;
                    right=j;
                }
            }
        }
        return s.substring(left,left+maxlen);
    }
}

反思

①String类型截取字符串函数为substring

标签:子串,int,maxlen,字符串,LeetCode,dp,回文
From: https://www.cnblogs.com/Janecodehouse/p/17027163.html

相关文章

  • Leetcode 重排链表 递归
    https://leetcode.com/problems/reorder-list/solutions/45113/Share-a-consise-recursive-solution-in-C++/https://leetcode.cn/problems/reorder-list/solutions/32910......
  • 【栈】LeetCode 1209. 删除字符串中的所有相邻重复项 II
    题目链接1209.删除字符串中的所有相邻重复项II思路用栈存储Pair<Character,Integer>,整数表示该字符连续出现的次数。遍历字符串s将其中的字符c依次压入栈顶并......
  • 【栈】LeetCode 1472. 设计浏览器历史记录
    题目链接1472.设计浏览器历史记录思路用栈history模拟网页的前进后退操作,用栈temp来暂时存储后退所退出的网页。代码classBrowserHistory{Stack<String>h......
  • [LeetCode] 2453. Destroy Sequential Targets
    Youaregivena 0-indexed array nums consistingofpositiveintegers,representingtargetsonanumberline.Youarealsogivenaninteger space.Youhave......
  • leetcode-653. 两数之和 IV - 输入二叉搜索树
    653.两数之和IV-输入二叉搜索树-力扣(Leetcode)用了迭代进行遍历二叉树,因为二叉搜索树的中序遍历是有序的,所以肯定左边大于右边,然后就可以用一个map来存放之前的数值,......
  • [LeetCode]015-三数之和
    >>>传送门题目给你一个整数数组nums,判断是否存在三元组[nums[i],nums[j],nums[k]]满足i!=j、i!=k且j!=k,同时还满足nums[i]+nums[j]+nums[k]==0......
  • leetcode-387-easy
    FirstUniqueCharacterinaStringGivenastrings,findthefirstnon-repeatingcharacterinitandreturnitsindex.Ifitdoesnotexist,return-1.Examp......
  • leetcode-414-easy
    ThirdMaximumNumberGivenanintegerarraynums,returnthethirddistinctmaximumnumberinthisarray.Ifthethirdmaximumdoesnotexist,returnthemaxim......
  • leetcode-563-easy
    BinaryTreeTiltGiventherootofabinarytree,returnthesumofeverytreenode'stilt.Thetiltofatreenodeistheabsolutedifferencebetweenthesum......
  • leetcode-349-easy
    IntersectionofTwoArraysGiventwointegerarraysnums1andnums2,returnanarrayoftheirintersection.Eachelementintheresultmustbeuniqueandyoum......