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 #373

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 6 commits into AlgorithmAndLeetCode:master from youngyangyang04:master
Nov 6, 2023
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
add: 0392.判断子序列 typescript 新增滚动数组解法
  • Loading branch information
xin authored and xin committed Oct 29, 2023
commit 74417c7695011c3c49d702f17cde22dfbce7aef3
27 changes: 27 additions & 0 deletions problems/0392.判断子序列.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,8 @@ const isSubsequence = (s, t) => {

### TypeScript:

> 二维数组

```typescript
function isSubsequence(s: string, t: string): boolean {
/**
Expand All @@ -236,6 +238,31 @@ function isSubsequence(s: string, t: string): boolean {
}
```

> 滚动数组
```typescript
function isSubsequence(s: string, t: string): boolean {
const sLen = s.length
const tLen = t.length
const dp: number[] = new Array(tLen + 1).fill(0)

for (let i = 1; i <= sLen; i++) {
let prev: number = 0;
let temp: number = 0;
for (let j = 1; j <= tLen; j++) {
// 备份一下当前状态(经过上层迭代后的)
temp = dp[j]
// prev 相当于 dp[j-1](累加了上层的状态)
// 如果单纯 dp[j-1] 则不会包含上层状态
if (s[i - 1] === t[j - 1]) dp[j] = prev + 1
else dp[j] = dp[j - 1]
// 继续使用上一层状态更新参数用于当前层下一个状态
prev = temp
}
}
return dp[tLen] === sLen
}
```

### Go:

二维DP:
Expand Down

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