当前位置:网站首页>LeetCode每日一题(516. Longest Palindromic Subsequence)
LeetCode每日一题(516. Longest Palindromic Subsequence)
2022-07-03 09:01:00 【wangjun861205】
Given a string s, find the longest palindromic subsequence’s length in s.
A subsequence is a sequence that can be derived from another sequence by deleting some or no elements without changing the order of the remaining elements.
Example 1:
Input: s = “bbbab”
Output: 4
Explanation: One possible longest palindromic subsequence is “bbbb”.
Example 2:
Input: s = “cbbd”
Output: 2
Explanation: One possible longest palindromic subsequence is “bb”.
Constraints:
- 1 <= s.length <= 1000
- s consists only of lowercase English letters.
dp[i][j]代表 s[i…=j]中的回文的长度, 当我们找到一对儿相同的字符 s[i] == s[k], 如果我们要把这一对儿字符放到回文里,那最终回文长度就是 dp[i+1][k-1] + 2, 如果我们要略过这一对儿字符, 则是 dp[i+1][j]
use std::collections::HashMap;
impl Solution {
fn dp(chars: &Vec<char>, i: usize, j: usize, cache: &mut HashMap<(usize, usize), i32>) -> i32 {
if i > j {
return 0;
}
if i == j {
return 1;
}
let mut next_j = j;
while i < next_j {
if chars[i] != chars[next_j] {
next_j -= 1;
continue;
}
break;
}
let pick = if i == next_j {
1
} else {
if let Some(c) = cache.get(&(i + 1, next_j - 1)) {
*c + 2
} else {
Solution::dp(chars, i + 1, next_j - 1, cache) + 2
}
};
let pass = if let Some(c) = cache.get(&(i + 1, j)) {
*c
} else {
Solution::dp(chars, i + 1, j, cache)
};
let ans = pick.max(pass);
cache.insert((i, j), ans);
ans
}
pub fn longest_palindrome_subseq(s: String) -> i32 {
let chars: Vec<char> = s.chars().collect();
Solution::dp(&chars, 0, chars.len() - 1, &mut HashMap::new())
}
}
边栏推荐
- The "booster" of traditional office mode, Building OA office system, was so simple!
- There is no open in default browser option in the right click of the vscade editor
- The server denied password root remote connection access
- MySQL installation and configuration (command line version)
- 【点云处理之论文狂读经典版13】—— Adaptive Graph Convolutional Neural Networks
- Beego learning - Tencent cloud upload pictures
- Overview of database system
- LeetCode 57. Insert interval
- 2022-2-14 learning the imitation Niuke project - send email
- Excel is not as good as jnpf form for 3 minutes in an hour. Leaders must praise it when making reports like this!
猜你喜欢

MySQL installation and configuration (command line version)

【点云处理之论文狂读经典版14】—— Dynamic Graph CNN for Learning on Point Clouds

LeetCode 715. Range module

Go language - Reflection

Spark 结构化流写入Hudi 实践
![[point cloud processing paper crazy reading frontier version 10] - mvtn: multi view transformation network for 3D shape recognition](/img/94/2ab1feb252dc84c2b4fcad50a0803f.png)
[point cloud processing paper crazy reading frontier version 10] - mvtn: multi view transformation network for 3D shape recognition
![[point cloud processing paper crazy reading frontier version 11] - unsupervised point cloud pre training via occlusion completion](/img/76/b92fe4549cacba15c113993a07abb8.png)
[point cloud processing paper crazy reading frontier version 11] - unsupervised point cloud pre training via occlusion completion

【Kotlin学习】运算符重载及其他约定——重载算术运算符、比较运算符、集合与区间的约定

【点云处理之论文狂读前沿版11】—— Unsupervised Point Cloud Pre-training via Occlusion Completion

Discussion on enterprise informatization construction
随机推荐
Hudi学习笔记(三) 核心概念剖析
On February 14, 2022, learn the imitation Niuke project - develop the registration function
[kotlin learning] classes, objects and interfaces - define class inheritance structure
[point cloud processing paper crazy reading classic version 13] - adaptive graph revolutionary neural networks
With low code prospect, jnpf is flexible and easy to use, and uses intelligence to define a new office mode
【毕业季|进击的技术er】又到一年毕业季,一毕业就转行,从动物科学到程序员,10年程序员有话说
Integrated use of interlij idea and sonarqube
npm install安装依赖包报错解决方法
Jenkins learning (I) -- Jenkins installation
Navicat, MySQL export Er graph, er graph
Common penetration test range
Overview of image restoration methods -- paper notes
【点云处理之论文狂读前沿版10】—— MVTN: Multi-View Transformation Network for 3D Shape Recognition
【Kotlin疑惑】在Kotlin类中重载一个算术运算符,并把该运算符声明为扩展函数会发生什么?
[point cloud processing paper crazy reading classic version 9] - pointwise revolutionary neural networks
【点云处理之论文狂读前沿版9】—Advanced Feature Learning on Point Clouds using Multi-resolution Features and Learni
图像修复方法研究综述----论文笔记
Vscode编辑器右键没有Open In Default Browser选项
Internet Protocol learning record
[point cloud processing paper crazy reading classic version 10] - pointcnn: revolution on x-transformed points