mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-09 19:44:45 +08:00
Update 1143.最长公共子序列.md
This commit is contained in:
@ -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
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@ -174,4 +202,4 @@ Go:
|
|||||||
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
|
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
|
||||||
* B站视频:[代码随想录](https://space.bilibili.com/525438321)
|
* B站视频:[代码随想录](https://space.bilibili.com/525438321)
|
||||||
* 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ)
|
* 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ)
|
||||||
<div align="center"><img src=../pics/公众号.png width=450 alt=> </img></div>
|
<div align="center"><img src=../pics/公众号.png width=450 alt=> </img></div>
|
||||||
|
Reference in New Issue
Block a user