Skip to content

Commit

Permalink
Merge pull request youngyangyang04#2298 from ShuangmingMa/master
Browse files Browse the repository at this point in the history
修改 0392.判断子序列.md Go二维DP格式,并增加Go一维DP解法
  • Loading branch information
youngyangyang04 authored Oct 11, 2023
2 parents 71477e6 + d9e08a1 commit f555182
Showing 1 changed file with 29 additions and 10 deletions.
39 changes: 29 additions & 10 deletions problems/0392.判断子序列.md
Original file line number Diff line number Diff line change
Expand Up @@ -240,26 +240,45 @@ function isSubsequence(s: string, t: string): boolean {

### Go:

二维DP:

```go
func isSubsequence(s string, t string) bool {
dp := make([][]int,len(s)+1)
for i:=0;i<len(dp);i++{
dp[i] = make([]int,len(t)+1)
dp := make([][]int, len(s) + 1)
for i := 0; i < len(dp); i ++{
dp[i] = make([]int, len(t) + 1)
}
for i:=1;i<len(dp);i++{
for j:=1;j<len(dp[i]);j++{
if s[i-1] == t[j-1]{
dp[i][j] = dp[i-1][j-1] +1
for i := 1; i < len(dp); i ++{
for j := 1; j < len(dp[i]); j ++{
if s[i - 1] == t[j - 1] {
dp[i][j] = dp[i - 1][j - 1] +1
}else{
dp[i][j] = dp[i][j-1]
dp[i][j] = dp[i][j - 1]
}
}
}
return dp[len(s)][len(t)]==len(s)
return dp[len(s)][len(t)] == len(s)
}
```

Rust:
一维DP:

```go
func isSubsequence(s string, t string) bool {
dp := make([]int, len(s) + 1)
for i := 1; i <= len(t); i ++ {
for j := len(s); j >= 1; j -- {
if t[i - 1] == s[j - 1] {
dp[j] = dp[j - 1] + 1
}
}
}
return dp[len(s)] == len(s)
}
```


### Rust:

```rust
impl Solution {
Expand Down

0 comments on commit f555182

Please sign in to comment.