当前位置:网站首页>回文相关题目
回文相关题目
2022-07-23 08:03:00 【ATTACH_Fine】
647. 回文子串
题目
给你一个字符串 s ,请你统计并返回这个字符串中 回文子串 的数目。回文字符 是正着读和倒过来读一样的字符串。子字符串是字符串中的由连续字符组成的一个序列。
具有不同开始位置或结束位置的子串,即使是由相同的字符组成,也会被视作不同的子串。
示例:
思路
1.确定dp数组(dp table)以及下标的含义
布尔类型的dp[i][j]:表示区间范围[i,j] (注意是左闭右闭)的子串是否是回文子串,如果是dp[i][j]为true,否则为false。
2. 确定递推公式
s[i]与s[j]不相等 return false;
s[i]与s[j]相等
情况一:下标i 与 j相同,同一个字符例如a,当然是回文子串 i==j
情况二:下标i 与 j相差为1,例如aa,也是回文子串 j - i == 1
情况三:下标:i 与 j相差大于1的时候,例如cabac,此时s[i]与s[j]已经相同了,我们看i到j区间是不是回文子串就看aba是不是回文就可以了,那么aba的区间就是 i+1 与 j-1区间,这个区间是不是回文就看**dp[i + 1][j - 1]**是否为true。
3.初始化
4.确定遍历顺序
情况三是根据dp[i + 1][j - 1]是否为true,在对dp[i][j]进行赋值true的。dp[i + 1][j - 1] 在 dp[i][j]的左下角,如图:
一定要从下到上,从左到右遍历,这样保证dp[i + 1][j - 1]都是经过计算的。
代码
class Solution {
public int countSubstrings(String s) {
//动态规划 dp[i][j] 代表[i,j]区间内是否是回文字符
//dp[i][j]由dp[i-1][j+1]决定
int len = s.length();
boolean[][] dp = new boolean[len][len];
int res = 0;
//从下到上,从左到右遍历
for(int i = len-1; i >= 0; i--){
for(int j = i; j < len; j++){
if(s.charAt(i) == s.charAt(j)){
//(s.charAt(i)==s.charAt(j) 时,当元素个数为1,2,3个时,一定为回文子串
if(j - i <= 1){
res++;
dp[i][j] = true;
}else if(dp[i+1][j-1] == true){
res++;
dp[i][j] = true;
}
}
}
}
return res;
}
}
516. 最长回文子序列
给你一个字符串 s ,找出其中最长的回文子序列,并返回该序列的长度。
子序列定义为:不改变剩余字符顺序的情况下,删除某些字符或者不删除任何字符形成的一个序列。
示例:
思路
1.确定dp数组(dp table)以及下标的含义
dp[i][j]:字符串s在**[i, j]范围**内最长的回文子序列的长度为dp[i][j]。
2.确定递推公式
在判断回文子串的题目中,关键逻辑就是看s[i]与s[j]是否相同。
(1)如果s[i]与s[j]相同,那么dp[i][j] = dp[i + 1][j - 1] + 2;
(2)如果s[i]与s[j]不相同,说明s[i]和s[j]的同时加入 并不能增加[i,j]区间回文子串的长度,那么分别加入s[i]、s[j]看看哪一个可以组成最长的回文子序列。
加入s[j]的回文子序列长度为dp[i + 1][j]。
加入s[i]的回文子序列长度为dp[i][j - 1]。
dp[i][j]一定是取最大的,即:dp[i][j] = max(dp[i + 1][j], dp[i][j - 1]);
3.初始化
首先要考虑当i 和j 相同的情况,从递推公式:dp[i][j] = dp[i + 1][j - 1] + 2; 可以看出 递推公式dp[i][j]是计算不到 i 和j相同时候的情况。
所以需要手动初始化一下,当i与j相同,那么dp[i][j]一定是等于1的,即:一个字符的回文子序列长度就是1。
其他情况dp[i][j]初始为0就行,这样递推公式:dp[i][j] = max(dp[i + 1][j], dp[i][j - 1]); 中dp[i][j]才不会被初始值覆盖。
4.确定遍历顺序
所以遍历i的时候一定要从下到上遍历,这样才能保证,下一行的数据是经过计算的。
代码
class Solution {
public int longestPalindromeSubseq(String s) {
//动态规划 dp[i][j]代表在[i,j]上最长的回文子序列的长度
int len = s.length();
int[][] dp = new int[len+1][len+1];
//从下到上,从左到右遍历
for(int i = len-1; i >= 0; i--){
for(int j = i+1; j < len; j++){
//当i和j相同时初始化
dp[i][i] = 1;
if(s.charAt(i) == s.charAt(j)){
dp[i][j] = dp[i+1][j-1] + 2;
}else{
dp[i][j] = Math.max(dp[i][j-1],dp[i+1][j]);
}
}
}
return dp[0][len-1];
}
}
边栏推荐
- Which is the difference between iqoo 10 pro and vivo X80 pro? Detailed parameter configuration comparison
- Where does pytorch work?
- iQOO 10 Pro和小米12 Ultra哪个好 哪个值得买 两者配置对比
- Excitation generator, monitor
- How to open the thought map pdf of postgraduate entrance examination in the small program of postgraduate entrance examination question bank
- Head products generated 2.5 billion yuan, and SLG track was also targeted by black products
- 第八天笔记
- How about Celeron n5095 Processor? What is the equivalent level of Intel n5095 core display
- js 实现通过身份证获取年龄
- Comment creo 9.0 modifie - t - il rapidement le système de coordonnées Cao?
猜你喜欢

STM32 output sine wave +cubemx configuration +hal Library

Notes on the fifth day

Notes on the seventh day

太平洋大西洋水流问题

链表复习!

英特尔赛扬7305性能怎么样?相当于什么水平级别

Tensor、Numpy、PIL格式转换以及图像显示

Day108. Shang Yitong: interface docking of hospital simulation system - query of hospital | Department | shift scheduling, addition, deletion, modification and paging conditions

网络安全笔记1——Internet协议的安全性

The difference between Celeron n4000 and Celeron n5095
随机推荐
Where does pytorch work?
js 实现 encode64 加密
赛扬n5095处理器怎么样 英特尔n5095核显相当于什么水平
ERP production operation control
数据库连接池 & DBUtils
iQOO 10 Pro和小米12 Ultra哪个好 哪个值得买 两者配置对比
JS数据类型判断方式总结
Kafka consumption reports an error coordinator unavailable Rediscovery will be attempt redisCovery
rtx3080ti和3090差距 rtx3080ti和3090哪个性价比高
Day 11 notes
鸡与蛋,产品与策略
小米12S Pro和小米12Pro天玑版区别 两者配置对比
AppScan的安装与使用
What level of rtx3070ti graphics card? What level of rtx3070ti graphics card? How about rtx3070ti graphics card
激励发生器、监测器
Rtx3080ti and rtx3080 gap 3080 and 3080ti parameter comparison
酷睿i7 1165g7相当于什么水平 i71165g7属于哪个档次
fastadmin更改默认表格按钮的弹窗大小
What is Tianji 920 equivalent to a snapdragon? How much is Tianji 920 equivalent to a snapdragon? How about Tianji 920
Configure the firetracker process, i.e. stepping on the pit record