当前位置:网站首页>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];
}
}
边栏推荐
- 【Harmony OS】【ARK UI】ets use startAbility or startAbilityForResult to invoke Ability
- Get the Ip tool class
- VR全景展打造专属元宇宙观展空间
- 普乐蛙VR台风体验馆厂家VR防震减灾模拟VR沉浸式体验设备
- 7.Keras开发简介
- 表的创建、修改与删除
- MCM box model modeling method and source analysis of atmospheric O3
- 在线密码生成工具推荐
- 接口测试框架实战(一) | Requests 与接口请求构造
- 【Harmony OS】【ARK UI】轻量级数据存储
猜你喜欢

私域流量时代来临,电商企业如何布局?

typescript39-class类的可见修饰符

GIS数据漫谈(五)— 地理坐标系统

【 Harmony OS 】 【 ano UI 】 lightweight data storage

Interface Test Framework Practice | Process Encapsulation and Test Case Design Based on Encrypted Interface

GIS数据漫谈(六)— 投影坐标系统

刚上线就狂吸70W粉,新型商业模式“分享购”来了,你知道吗?

数字孪生园区场景中的坐标知识

【HMS core】【Ads Kit】Huawei Advertising——Overseas applications are tested in China. Official advertisements cannot be displayed

Secondary development of WinForm controls
随机推荐
常见荧光染料修饰多种基团及其激发和发射波长数据一览数据
软件开发的最大的区别是什么?
修饰生物素DIAZO-生物素-PEG3-DBCO|重氮-生物素-三聚乙二醇-二苯基环辛炔
常见亲脂性细胞膜染料DiO, Dil, DiR, Did光谱图和实验操作流程
The flink sql task is changed, and after adding several fields to the sql, an error occurs when restoring from the previously saved savepoint.
typescript40-class类的保护修饰符
js中的闭包
Concepts and Methods of Exploratory Testing
接口和协议
IO进程线程->线程->day5
Unity2D horizontal board game tutorial 6 - enemy AI and attack animation
12.机器学习基础:评估机器学习模型
【Harmony OS】【ArkUI】ets开发 基础页面布局与数据连接
如何利用 Flutter 实现炫酷的 3D 卡片和帅气的 360° 展示效果
c语言结构体中的冒泡排序
DFS对剪枝的补充
传统企业如何转型社交电商,泰山众筹的玩法有哪些?
WinForm的控件二次开发
【生物素叠氮化物|cas:908007-17-0】价格_厂家
接口测试框架实战(二)| 接口请求断言
