当前位置:网站首页>图解LeetCode——5. 最长回文子串(难度:中等)
图解LeetCode——5. 最长回文子串(难度:中等)
2022-07-26 03:09:00 【爪哇缪斯】
一、题目
给你一个字符串 s,找到 s 中最长的回文子串。
二、示例
2.1> 示例 1:
【输入】s = "babad"
【输出】"bab"
【解释】"aba" 同样是符合题意的答案。
2.2> 示例 2:
【输入】s = "cbbd"
【输出】"bb"
提示:
1 <= s.length <= 1000s仅由数字和英文字母组成
三、解题思路
3.1> 通过中心点向两侧发散
因为本题是寻找最长回文字符串,所以,我们可以采取确定中心点之后,向两侧扩展的方式,来判断“扫描”到的子串是不是回文,如果是,则继续向两侧“扩展”,直到不满足回文条件为止。假设我们找到了一个回文字符串,其字符数存在两种情况,即:“奇数”字符个数和“偶数”字符个数。
对于奇数字符个数,中心点就是一个字符(即:head == tail),那么回文会以 2*N + 1的长度进行扩展。如下图所示:

对于偶数字符个数,中心点就是两个字符(即:head + 1 == tail),那么回文会以 2*N + 2的长度进行扩展。如下图所示:

整体遍历从头字符遍历到尾字符,在遍历过程中,当发现拼装的回文字符是现阶段最长的,则记录其mid和distinct(回文字符长度),当所有字符遍历完毕后,通过String的substring方法进行子字符串截图操作。
四、代码实现
4.1> 实现:通过中心点向两侧发散
class Solution {
public String longestPalindrome(String s) {
if (s == "" || s == null) {
return null;
}
int mid = 0, distinct = 0;
for (int i = 0; i < s.length(); i++) {
int temp = Math.max(search(s, i, i), search(s, i, i + 1));
mid = distinct < temp ? i : mid;
distinct = Math.max(distinct, temp);
}
int num = (distinct % 2 == 0) ? 1 : 0;
return distinct == 0 ? null : s.substring(mid - distinct/2 + num, mid + distinct/2 + 1);
}
/**
* 返回回文字符长度
* 如果返回是【偶数】,则说明"中心点"是两个元素,即:head + 1 == tail
* 如果返回是【奇数】,则说明"中心点"是一个元素,即:head == tail
*/
public int search(String s, int head, int tail) {
while (head >= 0 && tail < s.length() && s.charAt(head) == s.charAt(tail)) {
head--;
tail++;
}
return tail - head - 1;
}
}
今天的文章内容就这些了:
写作不易,笔者几个小时甚至数天完成的一篇文章,只愿换来您几秒钟的点赞&分享。
更多技术干货,欢迎大家关注公众号@爪哇缪斯「干货分享,每天更新」
题目来源:力扣(LeetCode)
边栏推荐
- Arthas download and startup
- Get twice the result with half the effort: learn the web performance test case design model
- Wechat official account mutual aid, open white groups, and small white newspaper groups to keep warm
- Anti electronic ink screen st7302
- Remember SQL optimization once
- Win11 method of changing disk drive letter
- YOLOv3: An Incremental Improvement
- Opencv 在图像上进行标注(画框+写字)
- YOLOv3: An Incremental Improvement
- STM32 - DMA notes
猜你喜欢

Anti electronic ink screen st7302

小测(一)

多线程编程

STM - exti external interrupt learning notes

YOLOv3: An Incremental Improvement

Opencv报错:(parameter or structure field))Unrecognized or unsupported array type in functon ‘cvGetMat‘

这种动态规划你见过吗——状态机动态规划之股票问题(上)

Execution process behind shell commands

图像识别(七)| 池化层是什么?有什么作用?

Pit trodden when copying list: shallow copy and deep copy
随机推荐
[detailed explanation of key and difficult points of document operation]
STM - exti external interrupt learning notes
复制列表时踩过的坑:浅拷贝与深拷贝
GoLang 抽奖系统 设计
Detailed explanation of extended physics informedneural networks paper
c语言分层理解(c语言函数)
Jenkins' study notes are detailed
Service gateway (zuul)
这种动态规划你见过吗——状态机动态规划之股票问题(上)
如何正确计算 Kubernetes 容器 CPU 使用率
[SQL] CASE表达式
事半功倍:学会WEB性能测试用例设计模型
js中数组排序的方法有哪些
[sql] case expression
File operation (I) -- File introduction and file opening and closing methods
YOLOv3: An Incremental Improvement
Personally test five efficient and practical ways to get rid of orders, and quickly collect them to help you quickly find high-quality objects!
手把手教你依赖管理
LeetCode·
[noip2001 popularization group] the problem of maximum common divisor and minimum common multiple