当前位置:网站首页>13.< tag-动态规划和回文字串>lt.647. 回文子串 + lt.516.最长回文子序列
13.< tag-动态规划和回文字串>lt.647. 回文子串 + lt.516.最长回文子序列
2022-08-03 04:55:00 【菜菜的大数据开发之路】
lt.647. 回文子串
[案例需求]

[思路分析一, 暴力解法]
[代码实现]
[思路分析二, 动态规划]
- 确定dp数组以及下标的含义
boolean dp[i][j]: 表示区间范围为[i, j] (注意事项左闭右闭)的字串是否是回文字串, 如果dp[i][j]为true, 否则为false;
- 确定递推公式
整体上是两种, 就是s[i]与s[j]相等, s[i]和s[j]不相等这两种.
当s[i]与s[j]不相等,那没啥好说的了,dp[i][j]一定是false。下标i 与 j相差为1,例如aa,也是回文子串下标:i 与 j相差大于1的时候,例如cabac,此时s[i]与s[j]已经相同了,我们看i到j区间是不是回文子串就看aba是不是回文就可以了,那么aba的区间就是 i+1 与 j-1区间,这个区间是不是回文就看dp[i + 1][j - 1]是否为true。
- 递推公式如下
if (s[i] == s[j]) {
if (j - i <= 1) {
// 情况一 和 情况二
result++;
dp[i][j] = true;
} else if (dp[i + 1][j - 1]) {
// 情况三
result++;
dp[i][j] = true;
}
}
- result就是统计回文子串的数量。
- 注意这里我没有列出当s[i]与s[j]不相等的时候,因为在下面dp[i][j]初始化的时候,就初始为false。
- dp数组初始化
dp[i][j]可以初始化为true么? 当然不行,怎能刚开始就全都匹配上了。所以dp[i][j]初始化为false。
- 确定遍历顺序
for (int i = s.size() - 1; i >= 0; i--) {
// 注意遍历顺序
for (int j = i; j < s.size(); j++) {
if (s[i] == s[j]) {
if (j - i <= 1) {
// 情况一 和 情况二
result++;
dp[i][j] = true;
} else if (dp[i + 1][j - 1]) {
// 情况三
result++;
dp[i][j] = true;
}
}
}
}
- 举例推导

[代码实现]
class Solution {
public int countSubstrings(String s) {
int len, ans = 0;
if (s == null || (len = s.length()) < 1) return 0;
//dp[i][j]:s字符串下标i到下标j的字串是否是一个回文串,即s[i, j]
boolean[][] dp = new boolean[len][len];
for (int j = 0; j < len; j++) {
for (int i = 0; i <= j; i++) {
//当两端字母一样时,才可以两端收缩进一步判断
if (s.charAt(i) == s.charAt(j)) {
//i++,j--,即两端收缩之后i,j指针指向同一个字符或者i超过j了,必然是一个回文串
if (j - i < 3) {
dp[i][j] = true;
} else {
//否则通过收缩之后的字串判断
dp[i][j] = dp[i + 1][j - 1];
}
} else {
//两端字符不一样,不是回文串
dp[i][j] = false;
}
}
}
//遍历每一个字串,统计回文串个数
for (int i = 0; i < len; i++) {
for (int j = 0; j < len; j++) {
if (dp[i][j]) ans++;
}
}
return ans;
}
}
lt.516.最长回文子序列
[案例需求]

[思路分析]
[代码实现]
public class Solution {
public int longestPalindromeSubseq(String s) {
int len = s.length();
int[][] dp = new int[len + 1][len + 1];
for (int i = len - 1; i >= 0; i--) {
// 从后往前遍历 保证情况不漏
dp[i][i] = 1; // 初始化
for (int j = i + 1; j < len; j++) {
if (s.charAt(i) == s.charAt(j)) {
dp[i][j] = dp[i + 1][j - 1] + 2;
} else {
dp[i][j] = Math.max(dp[i + 1][j], Math.max(dp[i][j], dp[i][j - 1]));
}
}
}
return dp[0][len - 1];
}
}
边栏推荐
- typescript44-对象之间的类兼容器
- Interface Test Framework Practice | Process Encapsulation and Test Case Design Based on Encrypted Interface
- OSI的分层特点、传输过程与三次握手、四次挥手、tcp与udp包头的描述
- typescript46-函数之间的类型兼容性
- 「短视频+社交电商」营销模式爆发式发展,带来的好处有什么?
- 2022/08/02 Study Notes (day22) Multithreading
- Bubble sort in c language structure
- 技术分享 | 接口自动化测试中如何对xml 格式做断言验证?
- 2.何为张量
- Two ways to simulate multi-user login in Jmeter
猜你喜欢

测试人员的价值体现在哪里

How to prepare for the test interface test data

Record some bugs encountered - when mapstruct and lombok are used at the same time, the problem of data loss when converting entity classes

如何利用 Flutter 实现炫酷的 3D 卡片和帅气的 360° 展示效果

接口测试框架实战(四)| 搞定 Schema 断言

内部类、static关键字、final

Coordinate knowledge in digital twin campus scenarios

UV decomposition of biotin - PEG2 - azide | CAS: 1192802-98-4 biotin connectors

6.神经网络剖析

在竞争白热化的电商行业,链动2+1为什么还有企业在用
随机推荐
「短视频+社交电商」营销模式爆发式发展,带来的好处有什么?
typescript46-函数之间的类型兼容性
Redis缓存雪崩、缓存穿透、缓存击穿
MySQL 入门:Case 语句很好用
【Harmony OS】【ARK UI】ets use startAbility or startAbilityForResult to invoke Ability
redis键值出现 xacxedx00x05tx00&的解决方法
Live | StarRocks technology insider: low base dictionary global optimization
私域流量时代来临,电商企业如何布局?
MySQL 删除表数据,重置自增 id 为 0 的两个方式
Create a tree structure
表的创建、修改与删除
The flink sql task is changed, and after adding several fields to the sql, an error occurs when restoring from the previously saved savepoint.
Jmeter 模拟多用户登录的两种方法
测试人员的价值体现在哪里
刚上线就狂吸70W粉,新型商业模式“分享购”来了,你知道吗?
Get the Ip tool class
【生物素叠氮化物|cas:908007-17-0】价格_厂家
Flink state
RequestContextHolder
私域流量引流方法?分享购火爆的商业模式,你值得拥有
