当前位置:网站首页>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];
}
}
边栏推荐
- 4.深度学习的几何解释与梯度的优化
- Interface testing framework combat (3) | JSON request and response assertion
- Redis缓存雪崩、缓存穿透、缓存击穿
- Shell之条件语句
- 【软件工程之美 - 专栏笔记】35 | 版本发布:软件上线只是新的开始
- BIOTIN ALKYNE CAS: 773888-45-2 Price, Supplier
- Jmeter 模拟多用户登录的两种方法
- typescript45-接口之间的兼容性
- 2.何为张量
- Record some bugs encountered - when mapstruct and lombok are used at the same time, the problem of data loss when converting entity classes
猜你喜欢

多肽介导PEG磷脂——靶向功能材料之DSPE-PEG-RGD/TAT/NGR/APRPG

Jmeter 模拟多用户登录的两种方法

How to use the interface management tool YApi?Beautiful, easy to manage, super easy to use

Super handy drawing tool is recommended

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

mysql 创建索引的三种方式

typescript49-交叉类型

【Harmony OS】【ARK UI】ets使用startAbility或startAbilityForResult方式调起Ability

DDL操作数据库、表、列

Secondary development of WinForm controls
随机推荐
2022暑假牛客多校联赛第一场
接口测试框架实战(二)| 接口请求断言
How to use the interface management tool YApi?Beautiful, easy to manage, super easy to use
Peptides mediated PEG DSPE of phospholipids, targeted functional materials - PEG - RGD/TAT/NGR/APRPG
Talking about GIS Data (6) - Projected Coordinate System
GIS数据漫谈(五)— 地理坐标系统
2.何为张量
【Harmony OS】【ARK UI】ets use startAbility or startAbilityForResult to invoke Ability
3.张量运算
1.一个神经网络示例
【生物素叠氮化物|cas:908007-17-0】价格_厂家
数据库基本概述与SQL概述
MySQL 删除表数据,重置自增 id 为 0 的两个方式
8.电影评论分类:二分类问题
js中的闭包
JS bottom handwriting
OSI的分层特点、传输过程与三次握手、四次挥手、tcp与udp包头的描述
修饰生物素DIAZO-生物素-PEG3-DBCO|重氮-生物素-三聚乙二醇-二苯基环辛炔
Get the Ip tool class
shell script loop statement
