当前位置:网站首页>暴力递归到动态规划 06 (剑指 Offer II 095. 最长公共子序列)
暴力递归到动态规划 06 (剑指 Offer II 095. 最长公共子序列)
2022-08-03 00:26:00 【涛涛英语学不进去】
1143.最长公共子序列
给定两个字符串 text1 和 text2,返回这两个字符串的最长公共子序列的长度。
一个字符串的 子序列 是指这样一个新的字符串:它是由原字符串在不改变字符的相对顺序的情况下删除某些字符(也可以不删除任何字符)后组成的新字符串。
例如,“ace” 是 “abcde” 的子序列,但 “aec” 不是 “abcde” 的子序列。两个字符串的「公共子序列」是这两个字符串所共同拥有的子序列。
若这两个字符串没有公共子序列,则返回 0。
示例 1:
输入:text1 = “abcde”, text2 = “ace”
输出:3
解释:最长公共子序列是 “ace”,它的长度为 3。
示例 2:
输入:text1 = “abc”, text2 = “abc”
输出:3
解释:最长公共子序列是 “abc”,它的长度为 3。
示例 3:
输入:text1 = “abc”, text2 = “def”
输出:0
解释:两个字符串没有公共子序列,返回 0。
提示:
- 1 <= text1.length <= 1000
- 1 <= text2.length <= 1000
输入的字符串只含有小写英文字符。
暴力递归
我讲不出来,谁讲出来麻烦在评论区说一下。
public int longestCommonSubsequence(String text1, String text2) {
return getLongLest(text1, text1.length() - 1, text2, text2.length() - 1);
}
private int getLongLest(String text1, int text1Index, String text2Str, int text2tIndex) {
if (text1Index == 0 && text2tIndex == 0) {
//比较当前位是否相等
return text1.charAt(text1Index) == text2Str.charAt(text2tIndex) ? 1 : 0;
} else if (text1Index == 0) {
//左边到头了就左边不动比右边
return text1.charAt(text1Index) == text2Str.charAt(text2tIndex) ? 1 : getLongLest(text1, text1Index, text2Str, text2tIndex - 1);
} else if (text2tIndex == 0) {
//右边到头了就右边不动比左边
return text1.charAt(text1Index) == text2Str.charAt(text2tIndex) ? 1 : getLongLest(text1, text1Index - 1, text2Str, text2tIndex);
} else {
//都不為0
int p1 = getLongLest(text1, text1Index, text2Str, text2tIndex - 1);
int p2 = getLongLest(text1, text1Index - 1, text2Str, text2tIndex);
//都没到头就比中间
int p3 = text1.charAt(text1Index) == text2Str.charAt(text2tIndex) ? 1 + getLongLest(text1, text1Index - 1, text2Str, text2tIndex - 1) : Integer.MIN_VALUE;
return Math.max(p1, Math.max(p2, p3));
}
}
动态规划
这个就好说多了
先初始化第一位,相等则为1,否则为0,初始化第一行,用a和每一个比较,相等则为1,否则为之前的数,为什么要为之前的数呢?自己想想啊,比如a和a比是1,那a和ab比呢,也是1啊。纵向也一样;
然后从ij1开始比,因为是双方互相比,如果当前位不相等,要从左边和上边取最大值,如果当前位相等,还要和左上 + 1 比取最大值。就是当前位是都往后挪一位,自然要用那个位置的数 + 1 了。
public int longestCommonSubsequence2(String text1, String text2) {
int m = text1.length();
int n = text2.length();
char[] s1 = text1.toCharArray();
char[] s2 = text2.toCharArray();
int[][] dp = new int[m][n];
//首位初始化
dp[0][0] = s1[0] == s2[0] ? 1 : 0;
//行列初始化
for (int i = 1; i < m; i++) {
dp[i][0] = s1[i] == s2[0] ? 1 : dp[i - 1][0];
}
for (int i = 1; i < n; i++) {
dp[0][i] = s1[0] == s2[i] ? 1 : dp[0][i - 1];
}
for (int i = 1; i < m; i++) {
for (int j = 1; j < n; j++) {
int left = dp[i][j - 1];
int up = dp[i - 1][j];
int cur = s1[i] == s2[j] ? 1 + dp[i - 1][j - 1] : 0;
dp[i][j] = Math.max(left, Math.max(up, cur));
}
}
return dp[m - 1][n - 1];
}

边栏推荐
- GTK实现水波纹效果
- 机电设备制造企业,如何借助ERP系统做好客供料管理?
- 线性DP
- 「PHP基础知识」隐式数据类型
- 和睦家私有化后换帅:新风天域吴启楠任CEO 李碧菁靠边站
- 2022 Shandong International Youth Eye Health Industry Exhibition, Vision Health Exhibition, Optometry Exhibition
- Carefully organize 16 MySQL usage specifications to reduce problems by 80% and recommend sharing with the team
- 【飞控开发高级教程2】疯壳·开源编队无人机-遥控整机代码走读、编译与烧写
- 定了!8月起,网易将为本号粉丝提供数据分析培训,费用全免!
- 吴恩达深度学习deeplearning.ai——第一门课:神经网络与深度学习——第二节:神经网络基础(上)
猜你喜欢
随机推荐
【QT】自定义工程封装成DLL并如何调用(带ui界面的)
@GetMapping、@PostMapping、@PutMapping、@DeleteMapping的区别
从一文中了解SSRF的各种绕过姿势及攻击思路
和睦家私有化后换帅:新风天域吴启楠任CEO 李碧菁靠边站
聊聊 Nacos
Heartwarming AI Review (1)
Introduction to resubmit Progressive Anti-Duplicate Submission Framework
如何快速对接淘宝开放平台API接口(淘宝店铺订单明文接口,淘宝店铺商品上传接口,淘宝店铺订单交易接口)
7.29
稳压电源: 电路图及类型
npm运行项目dependencies were not found: core-js/modules/es6.array.fill
NVM和NRM
1686. 石子游戏 VI
电压传感器: 工作原理、类型及电路图
Last Common Ancestor (LCA) Study Notes | P3379 【Template】Least Common Ancestor (LCA) Problem Solution
嵌入式分享合集26
MySQL的多表查询(1)
Vite教程 安装
pytest-常用运行参数
开源聚力,共创未来 | 麒麟信安祝贺openKylin首个体验版正式发布!










