class Solution: # 循环写法def longestCommonSubsequence(self, text1: str, text2: str) -> int:m = len(text1) + 1n = len(text2)+1dp=[[0]*n for _ in range(m)]for i in range(1,n): # 为什么要从1开始:因为dp[0][0]=0,从dp[1][1]开始比较for j in range(1,m): # 计算顺序是自底向上if text1[i]==text2[j]:dp[i][j]=dp[i-1][j-1]+1else:dp[i][j] = max(dp[i][j-1],dp[i-1][j])return dp[m-1][n-1]
