|
58 | 58 |
|
59 | 59 | <!-- 这里可写通用的实现逻辑 -->
|
60 | 60 |
|
| 61 | +动态规划法。 |
| 62 | + |
| 63 | +定义 `dp[i][j]` 表示 `text1[0:i-1]` 和 `text2[0:j-1]` 的最长公共子序列(闭区间)。 |
| 64 | + |
| 65 | +递推公式如下: |
| 66 | + |
| 67 | + |
| 68 | + |
61 | 69 | <!-- tabs:start -->
|
62 | 70 |
|
63 | 71 | ### **Python3**
|
64 | 72 |
|
65 | 73 | <!-- 这里可写当前语言的特殊实现逻辑 -->
|
66 | 74 |
|
67 | 75 | ```python
|
68 |
| - |
| 76 | +class Solution: |
| 77 | + def longestCommonSubsequence(self, text1: str, text2: str) -> int: |
| 78 | + m, n = len(text1), len(text2) |
| 79 | + dp = [[0] * (n + 1) for _ in range(m + 1)] |
| 80 | + for i in range(1, m + 1): |
| 81 | + for j in range(1, n + 1): |
| 82 | + if text1[i - 1] == text2[j - 1]: |
| 83 | + dp[i][j] = dp[i - 1][j - 1] + 1 |
| 84 | + else: |
| 85 | + dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]) |
| 86 | + return dp[-1][-1] |
69 | 87 | ```
|
70 | 88 |
|
71 | 89 | ### **Java**
|
72 | 90 |
|
73 | 91 | <!-- 这里可写当前语言的特殊实现逻辑 -->
|
74 | 92 |
|
75 | 93 | ```java
|
| 94 | +class Solution { |
| 95 | + public int longestCommonSubsequence(String text1, String text2) { |
| 96 | + int m = text1.length(), n = text2.length(); |
| 97 | + int[][] dp = new int[m + 1][n + 1]; |
| 98 | + for (int i = 1; i <= m; ++i) { |
| 99 | + for (int j = 1; j <= n; ++j) { |
| 100 | + if (text1.charAt(i - 1) == text2.charAt(j - 1)) { |
| 101 | + dp[i][j] = dp[i - 1][j - 1] + 1; |
| 102 | + } else { |
| 103 | + dp[i][j] = Math.max(dp[i - 1][j], dp[i][j - 1]); |
| 104 | + } |
| 105 | + } |
| 106 | + } |
| 107 | + return dp[m][n]; |
| 108 | + } |
| 109 | +} |
| 110 | +``` |
| 111 | + |
| 112 | +### **C++** |
| 113 | + |
| 114 | +```cpp |
| 115 | +class Solution { |
| 116 | +public: |
| 117 | + int longestCommonSubsequence(string text1, string text2) { |
| 118 | + int m = text1.size(), n = text2.size(); |
| 119 | + vector<vector<int>> dp(m + 1, vector<int>(n + 1)); |
| 120 | + for (int i = 1; i <= m; ++i) |
| 121 | + { |
| 122 | + for (int j = 1; j <= n; ++j) |
| 123 | + { |
| 124 | + if (text1[i - 1] == text2[j - 1]) |
| 125 | + { |
| 126 | + dp[i][j] = dp[i - 1][j - 1] + 1; |
| 127 | + } |
| 128 | + else |
| 129 | + { |
| 130 | + dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]); |
| 131 | + } |
| 132 | + } |
| 133 | + } |
| 134 | + return dp[m][n]; |
| 135 | + } |
| 136 | +}; |
| 137 | +``` |
76 | 138 |
|
| 139 | +### **Go** |
| 140 | +
|
| 141 | +```go |
| 142 | +func longestCommonSubsequence(text1 string, text2 string) int { |
| 143 | + m, n := len(text1), len(text2) |
| 144 | + dp := make([][]int, m+1) |
| 145 | + for i := 0; i <= m; i++ { |
| 146 | + dp[i] = make([]int, n+1) |
| 147 | + } |
| 148 | + for i := 1; i <= m; i++ { |
| 149 | + for j := 1; j <= n; j++ { |
| 150 | + if text1[i-1] == text2[j-1] { |
| 151 | + dp[i][j] = dp[i-1][j-1] + 1 |
| 152 | + } else { |
| 153 | + dp[i][j] = max(dp[i-1][j], dp[i][j-1]) |
| 154 | + } |
| 155 | + } |
| 156 | + } |
| 157 | + return dp[m][n] |
| 158 | +} |
| 159 | +
|
| 160 | +func max(a, b int) int { |
| 161 | + if a > b { |
| 162 | + return a |
| 163 | + } |
| 164 | + return b |
| 165 | +} |
77 | 166 | ```
|
78 | 167 |
|
79 | 168 | ### **...**
|
|
0 commit comments