Skip to content

Commit 617499f

Browse files
authored
Update 0115.不同的子序列.md
添加 Go 语言版本
1 parent 513ccbc commit 617499f

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
@@ -221,6 +221,30 @@ class SolutionDP2:
221221
```
222222

223223
Go:
224+
```go
225+
func numDistinct(s string, t string) int {
226+
dp:= make([][]int,len(s)+1)
227+
for i:=0;i<len(dp);i++{
228+
dp[i] = make([]int,len(t)+1)
229+
}
230+
// 初始化
231+
for i:=0;i<len(dp);i++{
232+
dp[i][0] = 1
233+
}
234+
// dp[0][j] 为 0,默认值,因此不需要初始化
235+
for i:=1;i<len(dp);i++{
236+
for j:=1;j<len(dp[i]);j++{
237+
if s[i-1] == t[j-1]{
238+
dp[i][j] = dp[i-1][j-1] + dp[i-1][j]
239+
}else{
240+
dp[i][j] = dp[i-1][j]
241+
}
242+
}
243+
}
244+
return dp[len(dp)-1][len(dp[0])-1]
245+
}
246+
```
247+
224248

225249
Javascript:
226250
```javascript

0 commit comments

Comments
 (0)