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

[pull] master from youngyangyang04:master #325

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
pull merged 7 commits into AlgorithmAndLeetCode:master from youngyangyang04:master
Aug 4, 2023
Merged
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 57 additions & 0 deletions problems/0115.不同的子序列.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,63 @@ 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()]
}
}
```

> 滚动数组

```rust
impl Solution {
pub fn num_distinct(s: String, t: String) -> i32 {
if s.len() < t.len() {
return 0;
}
let (s, t) = (s.into_bytes(), t.into_bytes());
// 对于 t 为空字符串,s 作为子序列的个数为 1(删除 s 所有元素)
let mut dp = vec![1; s.len() + 1];
for char_t in t {
// dp[i - 1][j - 1],dp[j + 1] 更新之前的值
let mut pre = dp[0];
// 当开始遍历 t,s 的前 0 个字符无法包含任意子序列
dp[0] = 0;
for (j, &char_s) in s.iter().enumerate() {
let temp = dp[j + 1];
if char_t == char_s {
dp[j + 1] = pre + dp[j];
} else {
dp[j + 1] = dp[j];
}
pre = temp;
}
}
dp[s.len()]
}
}
```


<p align="center">
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
Expand Down

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