当前位置:网站首页>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())
}
}
边栏推荐
- Database execution error: SQL_ mode only_ full_ group_ by:
- Numerical analysis notes (I): equation root
- 【点云处理之论文狂读前沿版12】—— Adaptive Graph Convolution for Point Cloud Analysis
- 【点云处理之论文狂读前沿版10】—— MVTN: Multi-View Transformation Network for 3D Shape Recognition
- Overview of image restoration methods -- paper notes
- Recommend a low code open source project of yyds
- LeetCode 30. Concatenate substrings of all words
- Crawler career from scratch (3): crawl the photos of my little sister ③ (the website has been disabled)
- 图像修复方法研究综述----论文笔记
- STM32F103 can learning record
猜你喜欢

LeetCode 535. Encryption and decryption of tinyurl

【点云处理之论文狂读前沿版13】—— GAPNet: Graph Attention based Point Neural Network for Exploiting Local Feature

LeetCode 30. Concatenate substrings of all words

Instant messaging IM is the countercurrent of the progress of the times? See what jnpf says

Spark 概述

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

LeetCode 515. Find the maximum value in each tree row

【点云处理之论文狂读经典版7】—— Dynamic Edge-Conditioned Filters in Convolutional Neural Networks on Graphs

Hudi learning notes (III) analysis of core concepts

What are the stages of traditional enterprise digital transformation?
随机推荐
npm install安装依赖包报错解决方法
Detailed steps of windows installation redis
Tag paste operator (#)
Notes on numerical analysis (II): numerical solution of linear equations
[point cloud processing paper crazy reading cutting-edge version 12] - adaptive graph revolution for point cloud analysis
2022-2-13 learning the imitation Niuke project - home page of the development community
Integrated use of interlij idea and sonarqube
Hudi learning notes (III) analysis of core concepts
Liteide is easy to use
How to check whether the disk is in guid format (GPT) or MBR format? Judge whether UEFI mode starts or legacy mode starts?
[advanced feature learning on point clouds using multi resolution features and learning]
Crawler career from scratch (3): crawl the photos of my little sister ③ (the website has been disabled)
【Kotlin学习】高阶函数的控制流——lambda的返回语句和匿名函数
Serializer rewrite: update and create methods
Redis learning (I)
Linxu learning (4) -- Yum and apt commands
LeetCode 715. Range module
[point cloud processing paper crazy reading frontier edition 13] - gapnet: graph attention based point neural network for exploring local feature
Move anaconda, pycharm and jupyter notebook to mobile hard disk
Powerdesign reverse wizard such as SQL and generates name and comment