Update 0115.不同的子序列.md about rust

This commit is contained in:
fwqaaq
2023-07-23 18:57:41 +08:00
committed by GitHub
parent eb632c6183
commit f0ac0b2efd

View File

@ -311,7 +311,31 @@ function numDistinct(s: string, t: string): number {
};
```
Rust:
```rust
impl Solution {
pub fn num_distinct(s: String, t: String) -> i32 {
if s.len() < t.len() {
return 0;
}
let mut dp = vec![vec![0; s.len() + 1]; t.len() + 1];
// i = 0, t 为空字符串s 作为子序列的个数为 1删除 s 所有元素)
dp[0] = vec![1; s.len() + 1];
for (i, char_t) in t.chars().enumerate() {
for (j, char_s) in s.chars().enumerate() {
if char_t == char_s {
// t 的前 i 个字符在 s 的前 j 个字符中作为子序列的个数
dp[i + 1][j + 1] = dp[i][j] + dp[i + 1][j];
continue;
}
dp[i + 1][j + 1] = dp[i + 1][j];
}
}
dp[t.len()][s.len()]
}
}
```
<p align="center">