mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-08 16:54:50 +08:00
Merge pull request #2197 from fwqaaq/patch-50
Update 0392.判断子序列.md about rust
This commit is contained in:
@ -259,9 +259,49 @@ func isSubsequence(s string, t string) bool {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
Rust:
|
||||||
|
|
||||||
|
```rust
|
||||||
|
impl Solution {
|
||||||
|
pub fn is_subsequence(s: String, t: String) -> bool {
|
||||||
|
let mut dp = vec![vec![0; t.len() + 1]; s.len() + 1];
|
||||||
|
for (i, char_s) in s.chars().enumerate() {
|
||||||
|
for (j, char_t) in t.chars().enumerate() {
|
||||||
|
if char_s == char_t {
|
||||||
|
dp[i + 1][j + 1] = dp[i][j] + 1;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
dp[i + 1][j + 1] = dp[i + 1][j]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
dp[s.len()][t.len()] == s.len()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
> 滚动数组
|
||||||
|
|
||||||
|
```rust
|
||||||
|
impl Solution {
|
||||||
|
pub fn is_subsequence(s: String, t: String) -> bool {
|
||||||
|
let mut dp = vec![0; t.len() + 1];
|
||||||
|
let (s, t) = (s.as_bytes(), t.as_bytes());
|
||||||
|
for &byte_s in s {
|
||||||
|
let mut pre = 0;
|
||||||
|
for j in 0..t.len() {
|
||||||
|
let temp = dp[j + 1];
|
||||||
|
if byte_s == t[j] {
|
||||||
|
dp[j + 1] = pre + 1;
|
||||||
|
} else {
|
||||||
|
dp[j + 1] = dp[j];
|
||||||
|
}
|
||||||
|
pre = temp;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
dp[t.len()] == s.len()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
<p align="center">
|
<p align="center">
|
||||||
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
|
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
|
||||||
|
Reference in New Issue
Block a user