当前位置:网站首页>【leetcode】516. Longest palindrome subsequence
【leetcode】516. Longest palindrome subsequence
2022-04-21 13:51:00 【Front end corner】
【leetcode】516. The longest palindrome subsequence
You can look at this question before you do it 【leetcode】647. Palindrome string
subject
Give you a string s , Find the longest palindrome subsequence , And return the length of the sequence .
Subsequence is defined as : Without changing the order of the remaining characters , A sequence formed by deleting some characters or not deleting any characters .
Example 1:
Input :s = “bbbab”
Output :4
explain : The longest possible palindrome subsequence is “bbbb” .
Example 2:
Input :s = “cbbd”
Output :2
explain : The longest possible palindrome subsequence is “bb” .
Tips :
1 <= s.length <= 1000
s It's only made up of lowercase letters
Ideas

Dynamic programming
- set up
dp[i][j]Indicates the interval[i,j]The length of the longest palindrome subsequence of isdp[i][j] - Recurrence relation is divided into 2 In this case
- When
s[i] === s[j]when , It is divided into 3 In this casej === i,dp[i][j] = 1j - i === 1, It's like'aa'This situation ,dp[i][j] = 2j - i > 1,dp[i][j] = dp[i+1][j-1] + 2
- When
s[i] !== s[j]when , To delete one of the two characters , Take the one with a large palindrome string length after deletion , namelydp[i][j] = Math.max(dp[i+1][j] , dp[i][j-1])
- When
dpThe array is initialized to 0 that will do- Traversal order from left to right , From bottom to top
Code

/** * @param {string} s * @return {number} */
var longestPalindromeSubseq = function(s) {
let len = s.length
let ans = 0
let dp = (new Array(len)).fill(false).map(x => (new Array(len)).fill(false))
for(let i = len - 1 ; i >= 0 ; i--){
for(let j = i ; j < len ; j++){
if(s[i] === s[j]){
if(j === i) dp[i][j] = 1
else if(j - i === 1) dp[i][j] = 2
else dp[i][j] = dp[i+1][j-1] + 2
}else{
dp[i][j] = Math.max(dp[i][j-1] , dp[i+1][j])
}
// Update maximum length
ans = Math.max(ans , dp[i][j])
}
}
return ans
};
Complexity
- Time complexity : O ( n 2 ) O(n^2) O(n2)
- Spatial complexity : O ( n 2 ) O(n^2) O(n2)
Pay attention to my special column , Update three times a day leetcode Answer key , Get stronger together !
版权声明
本文为[Front end corner]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204211346048579.html
边栏推荐
- Oracle 备份与用户解锁
- String string dynamic change (10 points): in the following procedures, the function of fun is to find the character with the largest ASCII code value in the string STR, move all the characters before
- 并发编程之深入理解
- Software engineering - Fundamentals
- 并发编程之CAS和Atomic原子操作类
- Detailed explanation of JVM memory allocation mechanism
- 易鲸捷钱库新特性之SQL级别HINT功能初见
- 自动化监控系统Prometheus&Grafana入门实战
- 认识系统服务
- Vagrant详细教程
猜你喜欢
随机推荐
Synchronized of concurrent locking mechanism
OpenLDAP使用ldapadd手动添加用户
工具函数---小数位处理
npm---package.json
JMM model of concurrent programming and three characteristics of concurrency
JVM內存分配機制詳解
New technology is coming again, embrace agp7 0, are you ready to say goodbye to transform?
About ` object Thoughts on the impossibility of calling clone() ` subclass
基于数值微分和误差反向传播的比较
常见卷积神经网络结构
NPM --- NPM configuration file
Unittest单元测试(二)
JDBC and database connection pool
vite.config 配置文件
Basic training process of image classification -- Based on mobilenet_ V3 as an example
Improving the efficiency of randomly generated sphere interference inspection by block division
mysql-连接查询成本和成本统计数据辨析
What does it mean that the AGP transform API is abandoned?
图像分类的训练基本过程——以MobileNet_v3为例
STM32单片机初学5-IIC通信驱动OLED屏幕









