当前位置:网站首页>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())
}
}
边栏推荐
- Build a solo blog from scratch
- Filter comments to filter out uncommented and default values
- WARNING: You are using pip ; however. Later, upgrade PIP failed, modulenotfounderror: no module named 'pip‘
- The server denied password root remote connection access
- Problems in the implementation of lenet
- Data mining 2021-4-27 class notes
- 【点云处理之论文狂读经典版13】—— Adaptive Graph Convolutional Neural Networks
- Beego learning - Tencent cloud upload pictures
- LeetCode 508. The most frequent subtree elements and
- LeetCode 30. Concatenate substrings of all words
猜你喜欢
【点云处理之论文狂读经典版7】—— Dynamic Edge-Conditioned Filters in Convolutional Neural Networks on Graphs
Just graduate student reading thesis
Crawler career from scratch (I): crawl the photos of my little sister ① (the website has been disabled)
Save the drama shortage, programmers' favorite high-score American drama TOP10
Wonderful review | i/o extended 2022 activity dry goods sharing
Discussion on enterprise informatization construction
2022-2-14 learning xiangniuke project - Session Management
【Kotlin学习】类、对象和接口——定义类继承结构
Principles of computer composition - cache, connection mapping, learning experience
Hudi 数据管理和存储概述
随机推荐
Instant messaging IM is the countercurrent of the progress of the times? See what jnpf says
The less successful implementation and lessons of RESNET
ERROR: certificate common name “www.mysql.com” doesn’t match requested host name “137.254.60.11”.
【点云处理之论文狂读前沿版8】—— Pointview-GCN: 3D Shape Classification With Multi-View Point Clouds
Problems in the implementation of lenet
IDEA 中使用 Hudi
ERROR: certificate common name “*.” doesn’t match requested ho
Principles of computer composition - cache, connection mapping, learning experience
【毕业季|进击的技术er】又到一年毕业季,一毕业就转行,从动物科学到程序员,10年程序员有话说
Spark structured stream writing Hudi practice
【Kotlin学习】类、对象和接口——带非默认构造方法或属性的类、数据类和类委托、object关键字
[set theory] order relation (chain | anti chain | chain and anti chain example | chain and anti chain theorem | chain and anti chain inference | good order relation)
Django operates Excel files through openpyxl to import data into the database in batches.
LeetCode 75. Color classification
We have a common name, XX Gong
Hudi integrated spark data analysis example (including code flow and test results)
Crawler career from scratch (II): crawl the photos of my little sister ② (the website has been disabled)
Filter comments to filter out uncommented and default values
LeetCode 438. Find all letter ectopic words in the string
Crawler career from scratch (3): crawl the photos of my little sister ③ (the website has been disabled)