当前位置:网站首页>5 sous - chaîne palindrome la plus longue (intervalle DP)
5 sous - chaîne palindrome la plus longue (intervalle DP)
2022-06-09 19:17:00 【ABCD Zy.】
1. Description du problème:
Je te donne une chaîne. s,Trouver s La plus longue chaîne de palindromes.
Exemple 1:
Entrée:s = "babad"
Produits:"bab"
Explication:"aba" C'est aussi la réponse à la question.
Exemple 2:
Entrée:s = "cbbd"
Produits:"bb"
Conseils:
1 <= s.length <= 1000
s Composé uniquement de chiffres et de lettres anglaises
Source::Boucle de force(LeetCode)
Liens:https://leetcode.cn/problems/longest-palindromic-substring/
2. Analyse des idées:
Le sujet est similaire àSous - séquence palindrome la plus longueLa question de,Et la restriction de substrat exige que la chaîne soit continue,Mais c'est une idée similaire,Parce qu'il s'agit également de résoudre le problème de la satisfaction d'une limite dans un intervalle,Donc vous pouvez utiliser l'intervalle dp C'est réglé., C'est juste qu'il y a une différence dans le calcul de l'état , Juste un petit ajustement ,Pour une longueur de 1 Donc la longueur de la chaîne palindrome est 1;Pour une longueur de 2 Le jugement substratif de s[i] == s[j],égal à 2;Longueur supérieure à 2 Le jugement substratif de s[i] == s[j] Et f(i + 1,j - 1) Plus grand que 0 Description s[i + 1:j - 1] C'est la chaîne palindrome .
3. Les codes sont les suivants::
python(Temps mort):
class Solution:
def longestPalindrome(self, s: str) -> str:
n = len(s)
f = [[0] * (n + 10) for i in range(n + 10)]
res = ""
# Intervalle classique dpCadre, Longueur de l'énumération
for l in range(1, n + 1):
# Début de l'énumérationi
i = 0
while i + l - 1 < n:
# j Le point de départ actuel est iLa longueur estl Fin de l'intervalle de
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
}边栏推荐
- How about opening an account for flush stock? Is it safe to open an account?
- Hash table distributed hash table (DHT) hash table
- Structure design of high pressure differential probe
- What physical stores will be more profitable in 2022? A small cost shop suitable for women, yeqifang is healthy
- Investment of 550million yuan! Merck announced to build an advanced semiconductor integration base in Zhangjiagang, China
- Desai wisdom number - line chart (smooth line chart): number of college entrance examination students in each province in 2022
- 精益产品开发体系最佳实践及原则
- US Secretary of Commerce: consider adding more Chinese enterprises to the "blacklist" and will not relax sanctions in the near future
- 音频 3A 处理实践,让你的应用更「动听」
- Implementation method of redis generating global unique ID
猜你喜欢

As a programming ape, do you really know how to operate Google browser

SQL exercise 4: string processing function

二叉树的按层遍历

IEDA 安装actiBPM插件

Structure design of high pressure differential probe

尽一份孝心,为家人做一个老人防摔报警系统
windows下mysql 8.0.27 安装配置图文教程
![Leetcode: Sword finger offer 56 - I. number of occurrences in the array [grouping XOR]](/img/87/ffcadcc4542b06e7f0da375178bbcc.png)
Leetcode: Sword finger offer 56 - I. number of occurrences in the array [grouping XOR]

Hash table distributed hash table (DHT) hash table

Visual display of cool 3D charts
随机推荐
视觉AI芯片厂商肇观电子获亿元融资,工银资本领投
Simple dialogue system - Overview
10 common high-frequency business scenarios that trigger IO bottlenecks
How about opening an account for flush stock? Is it safe to open an account?
Redis生成全局唯一ID的实现方法
德州仪器发函:下半年供需失衡将缓解!模拟芯片涨势终结?
国联期货开户安全吗?国联期货公司开户方法是什么?
Low code analysis and inventory: two misunderstandings need to be avoided in the application of low code in the banking industry
明细表1字符串拼接合并插入到明细表2SQL输出过程记录
Demagnetization and balance adjustment steps of oscilloscope current probe
Technology sharing | system architecture under test and data flow analysis
同花顺股票开户怎么样?开户安全吗?
Hash table distributed hash table (DHT) hash table
Intel, Samsung, Qualcomm, etc. will form a group to acquire arm?
Structure design of high pressure differential probe
音频 3A 处理实践,让你的应用更「动听」
10个常见触发IO瓶颈的高频业务场景
How to adjust the font size of SQL editor in dbaver
Meinong bio is about to be listed: its operation has been relatively stable in the past three years, and it is estimated that the over raised amount is about 100million yuan
金鱼哥RHCA回忆录:DO447管理清单--章节实验