Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit f0ac0b2

Browse files
authored
Update 0115.不同的子序列.md about rust
1 parent eb632c6 commit f0ac0b2

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

‎problems/0115.不同的子序列.md‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,31 @@ function numDistinct(s: string, t: string): number {
311311
};
312312
```
313313

314+
Rust:
314315

316+
```rust
317+
impl Solution {
318+
pub fn num_distinct(s: String, t: String) -> i32 {
319+
if s.len() < t.len() {
320+
return 0;
321+
}
322+
let mut dp = vec![vec![0; s.len() + 1]; t.len() + 1];
323+
// i = 0, t 为空字符串,s 作为子序列的个数为 1(删除 s 所有元素)
324+
dp[0] = vec![1; s.len() + 1];
325+
for (i, char_t) in t.chars().enumerate() {
326+
for (j, char_s) in s.chars().enumerate() {
327+
if char_t == char_s {
328+
// t 的前 i 个字符在 s 的前 j 个字符中作为子序列的个数
329+
dp[i + 1][j + 1] = dp[i][j] + dp[i + 1][j];
330+
continue;
331+
}
332+
dp[i + 1][j + 1] = dp[i + 1][j];
333+
}
334+
}
335+
dp[t.len()][s.len()]
336+
}
337+
}
338+
```
315339

316340

317341
<p align="center">

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /