当前位置:网站首页>图解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)
边栏推荐
- Neo4j 导入csv数据报错:Neo4j load csv error : Couldn‘t load the external resource
- Programming example of STM32 state machine -- fully automatic washing machine (Part 1)
- Win11更改磁盘驱动器号的方法
- 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
- Design of golang lottery system
- Longest Substring Without Repeating Characters
- FPGA_Vivado软件初次使用流程_超详细
- [C Advanced] deeply explore the storage of data (in-depth analysis + interpretation of typical examples)
猜你喜欢
![[SQL] CASE表达式](/img/05/1bbb0b5099443f7ce5f5511703477e.png)
[SQL] CASE表达式

Self-supervised learning method to solve the inverse problem of Fokker-Planck Equation

在混合云中管理数据库:八个关键注意事项

Digital commerce cloud DMS dealer management system solution: DMS system realizes business Omni channel and sales data collection

图像识别(六)| 激活函数

实现一个方法,找出数组中的第k大和第m大的数字相加之和

Opencv报错:(parameter or structure field))Unrecognized or unsupported array type in functon ‘cvGetMat‘
![[C Advanced] deeply explore the storage of data (in-depth analysis + interpretation of typical examples)](/img/1e/33f9cc9446dcad8cdb78babbb5a22c.jpg)
[C Advanced] deeply explore the storage of data (in-depth analysis + interpretation of typical examples)

万维网、因特网和互联网的区别

Personally test five efficient and practical ways to get rid of orders, and quickly collect them to help you quickly find high-quality objects!
随机推荐
[NOIP2001 普及组]装箱问题
Standardize your own debug process
重装Win7系统如何进行?
Longest Substring Without Repeating Characters
Neo4j 导入csv数据报错:Neo4j load csv error : Couldn‘t load the external resource
Win11 hide input method status bar method
Qt 信号在多层次对象间传递 多层嵌套类对象之间信号传递
【TensorFlow&PyTorch】图像数据增强API
[noip2001 popularization group] the problem of maximum common divisor and minimum common multiple
Oxycon 2022 network capture frontier conference is about to open!
hello world驱动(二)-初级版
My friend took 25koffer as soon as he learned automation test. When will my function test end?
Summary of Huawei virtualization fusioncompute knowledge points
STM32——串口学习笔记(一个字节、16位数据、字符串、数组)
[translation] cloud like internal load balancer for kubernetes?
Matlab simulation of vertical handover between MTD SCDMA and TD LTE dual networks
Detailed explanation of extended physics informedneural networks paper
Golang log programming system
复制列表时踩过的坑:浅拷贝与深拷贝
Usage of '...' in golang