当前位置:网站首页>LeetCode 5. 最长回文子串
LeetCode 5. 最长回文子串
2022-07-02 13:15:00 【_刘小雨】
给你一个字符串 s,找到 s 中最长的回文子串。
示例 1:
输入:s = “babad”
输出:“bab”
解释:“aba” 同样是符合题意的答案。
示例 2:
输入:s = “cbbd”
输出:“bb”
提示:
1 <= s.length <= 1000
s 仅由数字和英文字母组成
class Solution {
public:
string longestPalindrome(string s) {
/// 有一种马拉车算法, 只能做这个回文子串问题,比较偏
// 二分 + hash 难度升级,可处理百万级别的
// 暴力做法
// 回文子串 分为奇数和偶数
string re ;
for(int i = 0; i < s.size(); i++)
{
int l = i - 1, r = i + 1;
while(l >= 0 && r < s.size() && s[l] == s[r] ) l --, r ++;
if(re.size() < r - l - 1) re = s.substr(l + 1, r - l - 1); /// l + 1, r - 1
l = i, r= i + 1;
while(l >= 0 && r < s.size() && s[l] == s[r] ) l --, r ++;
if(re.size() < r - l - 1) re = s.substr(l + 1, r - l - 1);
}
return re;
}
};
边栏推荐
- Write your own CPU Chapter 11 - learning notes
- 618 reprise en profondeur: la méthode gagnante de la famille Haier Zhi
- 外企高管、连续创业者、瑜伽和滑雪高手,持续迭代重构的程序人生
- By asp Net core downloads files according to the path exception
- 中国信通院《数据安全产品与服务图谱》,美创科技实现四大板块全覆盖
- Aujourd'hui dans l'histoire: Alipay lance le paiement par code à barres; La naissance du père du système de partage du temps; La première publicité télévisée au monde...
- Thinking about absolute truth and relative truth
- Analyzing more than 7million R & D needs, it is found that these eight programming languages are the most needed in the industry!
- Bean configuration override in boot
- 潘多拉 IOT 开发板学习(RT-Thread)—— 实验2 RGB LED 实验(学习笔记)
猜你喜欢
去除router-link中的下划线
PyC file decompile
Recommended practice sharing of Zhilian recruitment based on Nebula graph
Foreign enterprise executives, continuous entrepreneurs, yoga and skiing masters, and a program life of continuous iteration and reconstruction
Analyzing more than 7million R & D needs, it is found that these eight programming languages are the most needed in the industry!
Effectively use keywords to increase Amazon sales
Mathematical analysis_ Notes_ Chapter 5: univariate differential calculus
做机器视觉哪个软件好?
[fluent] dart data type string type (string definition | string splicing | string API call)
Idea jar package conflict troubleshooting
随机推荐
Library management system (Shandong Agricultural University Curriculum Design)
618 reprise en profondeur: la méthode gagnante de la famille Haier Zhi
Unity Json 编写
触发器:Mysql实现一张表添加或删除一条数据,另一张表同时添加
Summary of multithreading and thread synchronization knowledge
JS learning notes - data types
618深度复盘:海尔智家的制胜方法论
ROW_NUMBER()、RANK()、DENSE_RANK区别
Which software is good for machine vision?
[Yu Yue education] reference materials of sensing and intelligent control technology of Nanjing University of Technology
IDEA中设置背景图片(超详细)
Headline | Asian control technology products are selected in the textile and clothing industry digital transformation solution key promotion directory of Textile Federation
结构体的内存对齐
[fluent] dart data type string type (string definition | string splicing | string API call)
What is the difference between self attention mechanism and fully connected graph convolution network (GCN)?
Today in history: Alipay launched barcode payment; The father of time-sharing system was born; The first TV advertisement in the world
Yyds dry inventory executor package (parameter processing function)
七一献礼:易鲸捷 “百日会战”完美收官 贵阳银行数据库提前封板
Classifier visual interpretation stylex: Google, MIT, etc. have found the key attributes that affect image classification
潘多拉 IOT 开发板学习(RT-Thread)—— 实验2 RGB LED 实验(学习笔记)