Merge pull request #156 from QuinnDK/添加1143最长公共子序列Go版本

添加1143最长公共子序列Go版本
This commit is contained in:
Carl Sun
2021-05-17 15:56:07 +08:00
committed by GitHub

View File

@ -166,6 +166,34 @@ class Solution:
Go Go
```Go
func longestCommonSubsequence(text1 string, text2 string) int {
t1 := len(text1)
t2 := len(text2)
dp:=make([][]int,t1+1)
for i:=range dp{
dp[i]=make([]int,t2+1)
}
for i := 1; i <= t1; i++ {
for j := 1; j <=t2; j++ {
if text1[i-1]==text2[j-1]{
dp[i][j]=dp[i-1][j-1]+1
}else{
dp[i][j]=max(dp[i-1][j],dp[i][j-1])
}
}
}
return dp[t1][t2]
}
func max(a,b int)int {
if a>b{
return a
}
return b
}
```