当前位置:网站首页>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())
}
}
边栏推荐
- Using Hudi in idea
- WARNING: You are using pip version 21.3.1; however, version 22.0.3 is available. Prompt to upgrade pip
- Overview of image restoration methods -- paper notes
- LeetCode 513. Find the value in the lower left corner of the tree
- 2022-2-13 learn the imitation Niuke project - Project debugging skills
- Matlab dichotomy to find the optimal solution
- Simple use of MATLAB
- Basic knowledge of network security
- AcWing 786. Number k
- Install third-party libraries such as Jieba under Anaconda pytorch
猜你喜欢

IDEA 中使用 Hudi

Sword finger offer II 029 Sorted circular linked list

In the digital transformation, what problems will occur in enterprise equipment management? Jnpf may be the "optimal solution"
![[point cloud processing paper crazy reading classic version 7] - dynamic edge conditioned filters in revolutionary neural networks on Graphs](/img/0a/480f1d1eea6f2ecf84fd5aa96bd9fb.png)
[point cloud processing paper crazy reading classic version 7] - dynamic edge conditioned filters in revolutionary neural networks on Graphs

Django operates Excel files through openpyxl to import data into the database in batches.

Spark structured stream writing Hudi practice

Move anaconda, pycharm and jupyter notebook to mobile hard disk

AcWing 787. Merge sort (template)

Modify idea code

Spark 结构化流写入Hudi 实践
随机推荐
Logstash+jdbc data synchronization +head display problems
2022-2-14 learning xiangniuke project - generate verification code
LeetCode 508. The most frequent subtree elements and
Save the drama shortage, programmers' favorite high-score American drama TOP10
Temper cattle ranking problem
Beego learning - Tencent cloud upload pictures
Just graduate student reading thesis
PowerDesigner does not display table fields, only displays table names and references, which can be modified synchronously
IDEA 中使用 Hudi
[point cloud processing paper crazy reading classic version 11] - mining point cloud local structures by kernel correlation and graph pooling
Sword finger offer II 029 Sorted circular linked list
[advanced feature learning on point clouds using multi resolution features and learning]
Install third-party libraries such as Jieba under Anaconda pytorch
[point cloud processing paper crazy reading classic version 9] - pointwise revolutionary neural networks
Explanation of the answers to the three questions
Overview of database system
Modify idea code
Crawler career from scratch (I): crawl the photos of my little sister ① (the website has been disabled)
[kotlin learning] classes, objects and interfaces - classes with non default construction methods or attributes, data classes and class delegates, object keywords
[set theory] order relation (eight special elements in partial order relation | ① maximum element | ② minimum element | ③ maximum element | ④ minimum element | ⑤ upper bound | ⑥ lower bound | ⑦ minimu