当前位置:网站首页>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())
}
}
边栏推荐
- dried food! What problems will the intelligent management of retail industry encounter? It is enough to understand this article
- 【Kotlin学习】高阶函数的控制流——lambda的返回语句和匿名函数
- Tag paste operator (#)
- 【点云处理之论文狂读前沿版9】—Advanced Feature Learning on Point Clouds using Multi-resolution Features and Learni
- Sword finger offer II 091 Paint the house
- AcWing 788. Number of pairs in reverse order
- LeetCode 438. Find all letter ectopic words in the string
- 【点云处理之论文狂读经典版9】—— Pointwise Convolutional Neural Networks
- LeetCode 515. Find the maximum value in each tree row
- LeetCode 508. The most frequent subtree elements and
猜你喜欢
Spark structured stream writing Hudi practice
Using Hudi in idea
Spark 概述
[kotlin puzzle] what happens if you overload an arithmetic operator in the kotlin class and declare the operator as an extension function?
Crawler career from scratch (II): crawl the photos of my little sister ② (the website has been disabled)
Hudi 快速体验使用(含操作详细步骤及截图)
There is no open in default browser option in the right click of the vscade editor
Navicat, MySQL export Er graph, er graph
The "booster" of traditional office mode, Building OA office system, was so simple!
Basic knowledge of network security
随机推荐
LeetCode 324. Swing sort II
Use the interface colmap interface of openmvs to generate the pose file required by openmvs mvs
The idea of compiling VBA Encyclopedia
Spark structured stream writing Hudi practice
Crawler career from scratch (IV): climb the bullet curtain of station B through API
Excel is not as good as jnpf form for 3 minutes in an hour. Leaders must praise it when making reports like this!
Go language - IO project
【点云处理之论文狂读前沿版11】—— Unsupervised Point Cloud Pre-training via Occlusion Completion
Simple use of MATLAB
Hudi学习笔记(三) 核心概念剖析
AcWing 787. Merge sort (template)
[point cloud processing paper crazy reading frontier version 11] - unsupervised point cloud pre training via occlusion completion
How to check whether the disk is in guid format (GPT) or MBR format? Judge whether UEFI mode starts or legacy mode starts?
2022-2-13 learning the imitation Niuke project - home page of the development community
低代码前景可期,JNPF灵活易用,用智能定义新型办公模式
Linxu learning (4) -- Yum and apt commands
Education informatization has stepped into 2.0. How can jnpf help teachers reduce their burden and improve efficiency?
[advanced feature learning on point clouds using multi resolution features and learning]
With low code prospect, jnpf is flexible and easy to use, and uses intelligence to define a new office mode
[point cloud processing paper crazy reading classic version 8] - o-cnn: octree based revolutionary neural networks for 3D shape analysis