forked from yuanguangxin/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
126bbd2
commit 17cca86
Showing
2 changed files
with
29 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package 动态规划.q1143_最长公共子序列; | ||
|
||
/** | ||
* 动态规划 dp[i + 1][j + 1] = Math.max(dp[i+1][j], dp[i][j+1]) o(m*n) | ||
* | ||
* 若题目为最长公共子串,则在c1,c2不相等时不做处理(赋值0),在遍历过程中记录最大值即可 | ||
*/ | ||
public class Solution { | ||
|
||
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 = 0; i < m; i++) { | ||
for (int j = 0; j < n; j++) { | ||
char c1 = text1.charAt(i); | ||
char c2 = text2.charAt(j); | ||
if (c1 == c2) { | ||
dp[i + 1][j + 1] = dp[i][j] + 1; | ||
} else { | ||
dp[i + 1][j + 1] = Math.max(dp[i + 1][j], dp[i][j + 1]); | ||
} | ||
} | ||
} | ||
return dp[m][n]; | ||
} | ||
} |