当前位置:网站首页>13. lt.647. Palindromic substring + lt.516. Longest palindrome subsequence
13. lt.647. Palindromic substring + lt.516. Longest palindrome subsequence
2022-08-03 05:10:00 【Caicai's big data development road】
lt.647. 回文子串
[案例需求]
[思路分析一, 暴力解法]
[代码实现]
[思路分析二, 动态规划]
- 确定dp数组以及下标的含义
boolean dp[i][j]: represents the range of[i, j] (Precautions Left closed and right closed)Whether the string of is a palindrome, 如果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];
}
}
边栏推荐
猜你喜欢
【HMS core】【Ads Kit】华为广告——海外应用在国内测试正式广告无法展示
typescript44-对象之间的类兼容器
【 Harmony OS 】 【 ano UI 】 lightweight data storage
MCM box model modeling method and source analysis of atmospheric O3
Common fluorescent dyes to modify a variety of groups and its excitation and emission wavelength data in the data
高可用 两地三中心
IO process thread -> thread -> day5
社交电商:链动2+1模式,为什么能在电商行业生存那么久?
2022/08/02 Study Notes (day22) Multithreading
探索性测试的概念及方法
随机推荐
rosbag工具plotjuggler无法打开rosbag的问题
VR全景展打造专属元宇宙观展空间
探索性测试的概念及方法
Flink state
内部类、static关键字、final
数字化时代,企业如何建立自身的云平台与商业模式的选择?
在树莓派上搭建属于自己的网页(2)
typescript44-对象之间的类兼容器
MySQL 出现 The table is full 的解决方法
CobalStrike(CS)基础超级详细版
力扣561. 数组拆分
阿里云对象存储oss私有桶生成链接
移动流量的爆发式增长,社交电商如何选择商业模式
UV decomposition of biotin - PEG2 - azide | CAS: 1192802-98-4 biotin connectors
Shell条件语句判断
CAD有生僻字如何打出来、如何提交软件相关问题或建议?
unity2D横板游戏教程6-敌人AI以及受击动画
Super handy drawing tool is recommended
DFS's complement to pruning
Create a tree structure