-
Notifications
You must be signed in to change notification settings - Fork 0
/
LongestCommonSubSequence2.java
52 lines (50 loc) · 1.62 KB
/
LongestCommonSubSequence2.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package algorithm.topics.dp;
/**
* <a href="https://leetcode.cn/problems/longest-common-subsequence">Longest Common SubSequence</a>
*
* @author marvin
* @version LongestCommonSubSequence2.java, v 0.1 2022/10/10 23:39 Exp $
*/
public class LongestCommonSubSequence2 {
/**
* 方法一:动态规划
*
* @param text1
* @param text2
* @return
*/
public int longestCommonSubsequence(String text1, String text2) {
int m = text1.length();
int n = text2.length();
int[][] dp = new int[m + 1][n + 1];
for (int i = 1; i <= m; i++) {
for (int j = 1; j <= n; j++) {
if (text1.charAt(i - 1) == text2.charAt(j - 1)) {
dp[i][j] = dp[i - 1][j - 1] + 1;
} else {
dp[i][j] = Math.max(dp[i - 1][j], dp[i][j - 1]);
}
}
}
return dp[m][n];
}
/**
* 方法二:递归
*
* @param text1
* @param text2
* @return
*/
public int longestCommonSubsequence2(String text1, String text2) {
if (text1.length() == 0 || text2.length() == 0) {
return 0;
}
if (text1.charAt(text1.length() - 1) == text2.charAt(text2.length() - 1)) {
return 1 + longestCommonSubsequence(text1.substring(0, text1.length() - 1),
text2.substring(0, text2.length() - 1));
} else {
return Math.max(longestCommonSubsequence(text1, text2.substring(0, text2.length() - 1)),
longestCommonSubsequence(text1.substring(0, text1.length() - 1), text2));
}
}
}