当前位置:网站首页>返回字符串中的最大回文数
返回字符串中的最大回文数
2022-08-04 02:25:00 【pshdhx_albert】
/** * @author pshdhx * @date 2022-08-03 15:20 * @Des 给你一个字符串 s,找到 s 中最长的回文子串。 * 输入:s = "babad" * 输出:"bab" * 解释:"aba" 同样是符合题意的答案。 * <p> * 输入:s = "cbbd" * 输出:"bb" * @Method * 如果一个字符串是回文串,那么去掉他的首尾字母仍然是回文串; * @Summary */
package com.pshdhx.Algorithm.middle;
/**
* @author pshdhx
* @date 2022-08-03 15:20
* @Des 给你一个字符串 s,找到 s 中最长的回文子串。
* 输入:s = "babad"
* 输出:"bab"
* 解释:"aba" 同样是符合题意的答案。
* <p>
* 输入:s = "cbbd"
* 输出:"bb"
* @Method
* 如果一个字符串是回文串,那么去掉他的首尾字母仍然是回文串;
* @Summary
*/
public class 最长回文子串 {
public String longestPalindrome(String s) {
int len = s.length();
if (len < 2) {
return s;
}
int maxLen = 1;
int begin = 0;
// dp[i][j] 表示 s[i..j] 是否是回文串
boolean[][] dp = new boolean[len][len];
// 初始化:所有长度为 1 的子串都是回文串
for (int i = 0; i < len; i++) {
dp[i][i] = true;
}
char[] charArray = s.toCharArray();
// 递推开始
for (int L = 2; L <= len; L++) {//回文串长度
for (int i = 0; i < len; i++) {//回文串左边界
// 由 L 和 i 可以确定右边界,即 j - i + 1 = L 得
int j = L + i - 1;//回文串右边界
// 如果右边界越界,就可以退出当前循环【注意:右边界是从0开始的呀,不可能大于等于数组的长度】
if (j >= len) {
break;
}
if (charArray[i] != charArray[j]) {//如果左边界 != 右边界
dp[i][j] = false;
} else {// 左边界==右边界
if (j - i < 3) {//回文串长度小于4===》 【1,2,3】 如果是2:左边界=右边界,true;如果是3,则左边界=右边界,true,不用管中间是什么!
dp[i][j] = true;
} else {
dp[i][j] = dp[i + 1][j - 1];
}
}
// 只要 dp[i][L] == true 成立,就表示子串 s[i..L] 是回文,此时记录回文长度和起始位置
if (dp[i][j] && j - i + 1 > maxLen) {
maxLen = j - i + 1;
begin = i;
}
}
}
return s.substring(begin, begin + maxLen);
}
public static void main(String[] args) {
String babad = new 最长回文子串().longestPalindrome("babad");
System.out.println(babad);
}
}
边栏推荐
- Countdown to 2 days, the "New Infrastructure of Cultural Digital Strategy and Ecological Construction of Cultural Art Chain" will kick off soon
- priority_queue元素为指针时,重载运算符失效
- 验证码业务逻辑漏洞
- [Original] Start the XPS/OXPS reader that comes with Windows 10
- 工程制图平面投影练习
- halcon自定义函数基本操作
- 实例036:算素数
- Presto中broadcast join和partition join执行计划的处理过程
- 共n级台阶,每次可以上1级或2级台阶,有多少种上法?
- 工程制图复习题(带答案)
猜你喜欢
持续投入商品研发,叮咚买菜赢在了供应链投入上
MySQL高级-读写分离-分库分表
lombok注解@RequiredArgsConstructor的使用
In the season of going overseas, the localization of Internet tips for going overseas
DHCP服务详解
DDTL: Domain Transfer Learning at a Distance
Priority_queue element as a pointer, the overloaded operators
实例037:排序
2022广东省安全员A证第三批(主要负责人)考试题库及模拟考试
ssh服务详解
随机推荐
实例039:有序列表插入元素
持续投入商品研发,叮咚买菜赢在了供应链投入上
Flask Framework Beginner-05-Command Management Manager and Database Use
Continuing to invest in product research and development, Dingdong Maicai wins in supply chain investment
2022焊工(初级)上岗证题目模拟考试平台操作
html select tag assignment database query result
Day13 Postman的使用
LeetCode:899. 有序队列【思维题】
DDTL:远距离的域迁移学习
There are n steps in total, and you can go up to 1 or 2 steps each time. How many ways are there?
Instance, 038: the sum of the diagonal matrix
关联接口测试
In a more general sense, calculating the displacement distance and assumptions
Good bosses, please ask the flink CDC oracle to Doris, found that the CPU is unusual, a run down
Continuing to pour money into commodities research and development, the ding-dong buy vegetables in win into the supply chain
Qt中对象树的机制介绍以及底层实现,各种结果分析:(以及自己写容易犯错的点)
QNX Hypervisor] 10.2 vdev 8259 2.2 user manual
Rongyun "Audio and Video Architecture Practice" technical session [complete PPT included]
瑞能微计量芯片RN2026的实用程序
Flask框架初学-05-命令管理Manager及数据库的使用