当前位置:网站首页>5 longest palindrome substring (interval DP)
5 longest palindrome substring (interval DP)
2022-06-09 19:18:00 【yuzhang_ zy】
1. Problem description :
Give you a string s, find s The longest palindrome substring in .
Example 1:
Input :s = "babad"
Output :"bab"
explain :"aba" It's the same answer .
Example 2:
Input :s = "cbbd"
Output :"bb"
Tips :
1 <= s.length <= 1000
s It consists only of numbers and English letters
source : Power button (LeetCode)
link :https://leetcode.cn/problems/longest-palindromic-substring/
2. Thought analysis :
This topic is similar to The longest palindrome subsequence The problem of , The substring restriction requires that the string be continuous , But it's a similar idea , Because it is also a problem of solving a certain limit in an interval , So you can use intervals dp To solve , It's just that there are some differences in state calculation , Just a little adjustment , For length is 1 The length of the palindrome string is 1; For length is 2 Substring judgment of s[i] == s[j], Equal is 2; Longer than 2 Substring judgment of s[i] == s[j] also f(i + 1,j - 1) Greater than 0 explain s[i + 1:j - 1] Is the palindrome string .
3. The code is as follows :
python( Overtime ):
class Solution:
def longestPalindrome(self, s: str) -> str:
n = len(s)
f = [[0] * (n + 10) for i in range(n + 10)]
res = ""
# Classical interval dp frame , Enumeration length
for l in range(1, n + 1):
# Enumeration starting point i
i = 0
while i + l - 1 < n:
# j The current starting point is i The length is l The end of the interval
j = i + l - 1
if l == 1:
f[i][j] = 1
elif l == 2:
if s[i] == s[j]:
f[i][j] = 2
else:
if s[i] == s[j] and f[i + 1][j - 1]:
f[i][j] = f[i + 1][j - 1] + 2
if f[i][j] > len(res):
res = s[i: j + 1]
i += 1
return resgo:
package main
import "fmt"
func longestPalindrome(s string) string {
n := len(s)
f := make([][]int, n+10)
for i := 0; i < n+10; i++ {
f[i] = make([]int, n+10)
}
res := ""
for l := 1; l <= n; l++ {
i := 0
for ; i+l-1 < n; i++ {
j := i + l - 1
if l == 1 {
f[i][j] = 1
} else if l == 2 {
if s[i] == s[j] {
f[i][j] = 2
}
} else if s[i] == s[j] && f[i+1][j-1] > 0 {
f[i][j] = f[i+1][j-1] + 2
}
if f[i][j] > len(res) {
res = s[i : j+1]
}
}
}
return res
}边栏推荐
- June team learning plan!
- 一季度全球PC GPU出货量下滑6.2%,疫情创造大量需求至此结束
- 企业内部Wiki,你建立了么?
- 软件测试是做什么的?具体工作内容?
- 德州仪器发函:下半年供需失衡将缓解!模拟芯片涨势终结?
- Krypton Evening News - Apple won the lawsuit, and the US judge rejected the class action on iPhone and iPad security defects; It is reported that JD will pilot the catering takeout business
- MySQL优化教程之慢查询日志实践
- 10个常见触发IO瓶颈的高频业务场景
- Singular Value Decomposition(SVD)
- Troubleshooting cl210openstack operations -- diagnosing openstack problems
猜你喜欢

Pro 后台子管理员 403 问题分析
Implementation method of redis generating global unique ID

Tpami2022 | heterogeneous domain adaptation based on adversarial neural representation learning: e-commerce and network security experiment
windows下mysql 8.0.27 安装配置图文教程

Have you established an internal wiki?

二叉树的按层遍历

Betting on cloud nativity, ampere computing starts the key shot of server chip transformation

24个月暴涨180万名开发者,Rust 迎来高光时刻

尽一份孝心,为家人做一个老人防摔报警系统

Qt数据库应用21-数据分组导出
随机推荐
尽一份孝心,为家人做一个老人防摔报警系统
KVM virtualization Fundamentals
155_ Model_ Safety stock of power Bi & power pivot purchase, sales and inventory
Technology sharing | system architecture under test and data flow analysis
Fast finding the number of nodes in a complete binary tree
德州仪器发函:下半年供需失衡将缓解!模拟芯片涨势终结?
深入浅出对话系统——概述
迪赛智慧数——折线图(平滑折线图):2022年各省高考生人数
一季度全球PC GPU出货量下滑6.2%,疫情创造大量需求至此结束
What physical stores will be more profitable in 2022? A small cost shop suitable for women, yeqifang is healthy
minikube 部署使用
Application of oscilloscope probe in switch loss test scheme
Traversal by layer of binary tree
Implementation method of redis generating global unique ID
Invest 400million euros! Intel and Spanish Supercomputing Center develop risc-v processor, which will be used for 100 billion times of supercomputing
Convenience bee was notified of its illegal collection of personal information, and its affiliated companies repeatedly received fines
二叉树的按层遍历
金鱼哥RHCA回忆录:DO447管理清单--章节实验
Zhaoguan electronics, a visual AI chip manufacturer, obtained a financing of 100 million yuan, led by ICBC capital
《数字经济全景白皮书》银行财富管理篇 重磅发布