33

LeetCode(6) 最长回文字符串

 4 years ago
source link: https://studygolang.com/articles/31389
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

题目:

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

思路:

看到这道题目,首先想到回文字符串,是一个沿正中字符串对称的字符串,一个字符串如果时回文字符串,去除两端的字符串也必为回文字符串,由此设中间的字符串为子状态,应该可以用动态规划的方式求解。这里设置储存状态的数组为dpi,为字符串(i~j)子串是否为回文字符串。编程思路如下:

MZrqy2m.png!mobile

具体代码如下所示:

定义了一个paliInfo结构体,用于储存最长子串的信息。相比起光放给出的算法删除了对于j>i部分的循环。实际的运行结果中,也体现出了这一点,比起官方的节省了差不多一半的时间,和空间。

VNBJrir.png!mobile

官方动态规划运行结果

z2auI3Y.png!mobile

个人的动态规划结果

type paliInfo struct {
   size int
 start int
 end int
}
func longestPalindrome(s string) string{
   //1.设定数组保存回文字符串信息
 n := len(s)
   dp := make([][]bool,n)
   for k := 0;k<n;k++{
      dp[k] = make ([]bool,n)
   }
   var ans paliInfo
 //2.如果一个字符串中i~j为回文字符串,则(i+1)~(j-1)为回文字符串
 //3.第一个字符和第二个字符的回文字符串不可以用此判断,设置初始值
 for i:=0;i<n;i++{
      dp[i][i] = true
 }
   ans.size = 1
 ans.start = 0
 ans.end = 0
 for i := 0;i<n;i++{
      for j := i-1;j>=0;j--{
         if i-j == 1{
            if s[i] == s[j]{
               dp[j][i] = true
 if i-j+1>ans.size{
                  ans.start = j
                  ans.end = i
                  ans.size =i-j+1
 }
            } else {
               dp[j][i]=false
 }
         }
         if i-j >1{
            if dp[j+1][i-1]&&s[i]==s[j]{
               dp[j][i]=true
 if i-j+1>ans.size{
                  ans.start = j
                  ans.end = i
                  ans.size =i-j+1
 }
            }else {
               dp[j][i]=false
 }
         }
      }
   }
   return s[ans.start:ans.end+1]
}

有疑问加站长微信联系

iiUfA3j.png!mobile

Recommend

  • 50
    • 掘金 juejin.im 6 years ago
    • Cache

    每日一算--最长回文子串

    题目 给定一个字符串 s,找到 s 中最长的回文子串。你可以假设 s 的最大长度为 1000。 示例1 输入: "babad" 输出: "bab" 注意: "aba" 也是一个有效答案。 复制代码 示例2 输入: "cbbd" 输出: "bb" 复制代码

  • 36

    刷力扣的时候, 碰到了一个找最长回文字符串的题, 解开了, 但只要提交就提示超时, 然后就开始想办法优化, 下面说一下我处理的方法 有兴...

  • 10
    • www.kirito41dd.cn 3 years ago
    • Cache

    最长回文子串-马拉车

    回文串是指是正着读和反着读都一样的字符串,比如abcba。 最长回文子串是指在一个字符串中能找到的最长回文串,如这个字符串最长回文字串是最后6个字符:abacacbaaaab 用马拉车算法求一个串中的最长回文子串:...

  • 4

    【面试现场】如何找到字符串中的最长回文子串?

  • 8
    • segmentfault.com 3 years ago
    • Cache

    最长回文子串 -- 三种解答

    给你一个字符串 s,找到 s 中最长的回文子串。示例 1: 输入:s = "babad" 输出:"bab" 解释:"aba" 同样是符合题意的答案。 示例 2: 输入:s = "cbbd" 输出:"bb" 示例 3: 输入:s = "a" 输出:"a" 示例 4: 输入:s = "ac" 输出:"a"...

  • 4
    • blog.feelyou.top 3 years ago
    • Cache

    【leetcode】680.验证回文字符串Ⅱ

    【leetcode】680.验证回文字符串Ⅱ 2020-03-07 链接:https://leetcode-cn.com/problems/valid-palindrome-ii 给定一个非空字符串 s,最多删除一个...

  • 8
    • www.purewhite.io 3 years ago
    • Cache

    LintCode 627.最长回文串

    LintCode 627. 最长回文串 发表于 2018-03-30 更新于 2021-12-02 分类于 算法 阅读次数: Disqus: 本文字数: 868 阅读时长 ≈ 2 分钟给出一个包...

  • 11
    • waiterxiaoyy.github.io 3 years ago
    • Cache

    LeetCode学习笔记——最长回文子串

    LeetCode学习笔记——最长回文子串

  • 6

    #yyds干货盘点# leetcode算法题:最长回文串 原创 灰太狼_cxh 2022-08-03...

  • 14
    • leetcode.cn 2 years ago
    • Cache

    5. 最长回文子串

    5. 最长回文子串 - 力扣(LeetCode)5. 最长回文子串给你一个字符串 s,找到 s 中最长的回文子串。

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK