From 4822d8ff8cb71d4787a4d6a3f33ecba98fbc9be3 Mon Sep 17 00:00:00 2001 From: fwqaaq Date: Tue, 25 Jul 2023 00:45:10 +0800 Subject: [PATCH] =?UTF-8?q?Update=200516.=E6=9C=80=E9=95=BF=E5=9B=9E?= =?UTF-8?q?=E6=96=87=E5=AD=90=E5=BA=8F=E5=88=97.md=20about=20rust?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0516.最长回文子序列.md | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/problems/0516.最长回文子序列.md b/problems/0516.最长回文子序列.md index fcdd57b0..2180bab8 100644 --- a/problems/0516.最长回文子序列.md +++ b/problems/0516.最长回文子序列.md @@ -274,7 +274,26 @@ function longestPalindromeSubseq(s: string): number { }; ``` +Rust: +```rust +impl Solution { + pub fn longest_palindrome_subseq(s: String) -> i32 { + let mut dp = vec![vec![0; s.len()]; s.len()]; + for i in (0..s.len()).rev() { + dp[i][i] = 1; + for j in i + 1..s.len() { + if s[i..=i] == s[j..=j] { + dp[i][j] = dp[i + 1][j - 1] + 2; + continue; + } + dp[i][j] = dp[i + 1][j].max(dp[i][j - 1]); + } + } + dp[0][s.len() - 1] + } +} +```