当前位置:网站首页>图解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)
边栏推荐
- How to close the case prompt icon of win11? Closing method of win11 case prompt Icon
- Image recognition (VII) | what is the pooling layer? What's the effect?
- YOLOv3: An Incremental Improvement
- 朋友刚学完自动化测试就拿25Koffer,我功能测试何时才能到头?
- Longest Substring Without Repeating Characters
- Win11麦克风权限的开启方法
- [translation] announce Vites 13
- 一切的源头,代码分支策略的选择
- How to reinstall win7 system?
- [NOIP2001 普及组] 最大公约数和最小公倍数问题
猜你喜欢

Jenkins' study notes are detailed

【无标题】

(九)属性自省
![[sql] usage of self connection](/img/92/92474343b4b4e6ea60453b4799cb55.jpg)
[sql] usage of self connection

Shardingsphere data slicing

Cycle and branch (I)

Arthas' dynamic load class (retransform)

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

The difference between the world wide web, the Internet and the Internet

C language layered understanding (C language function)
随机推荐
【尤里复裂人】带你轻松理解——深拷贝和浅拷贝
Three years of software testing experience, salary has been stuck at 10K, how to improve and develop automated testing?
移位距离和假设的应用
How can users create data tables on Web pages and store them in the database
Usage of arguments.callee
LeetCode·83双周赛·6128.最好的扑克手牌·模拟
How to install with USB flash disk?
canvas——绘制曲线——挂钟,饼图,五角星
Cloud native guide what is cloud native infrastructure
JSD-2204-酷鲨商城(管理商品模块)-Day02
Wechat official account mutual aid, open white groups, and small white newspaper groups to keep warm
Longest Substring Without Repeating Characters
STM32 - PWM learning notes
Small test (I)
The difference between the world wide web, the Internet and the Internet
Digital commerce cloud DMS dealer management system solution: DMS system realizes business Omni channel and sales data collection
Get twice the result with half the effort: learn the web performance test case design model
26 points that must be paid attention to for stability test
中国信通院陈屹力:降本增效是企业云原生应用的最大价值
图像识别(六)| 激活函数