当前位置:网站首页>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;
}
};
边栏推荐
- What if the win11 app store cannot load the page? Win11 store cannot load page
- Leetcode --- longest public prefix
- 自注意力机制和全连接的图卷积网络(GCN)有什么区别联系?
- Yyds dry inventory executor package (parameter processing function)
- By asp Net core downloads files according to the path exception
- Best practices for building multi architecture images
- What is Amazon keyword index? The consequences of not indexing are serious
- 图书管理系统(山东农业大学课程设计)
- Yyds dry goods inventory hands-on teaching you to carry out the packaging and release of mofish Library (fishing Library)
- 理想之光不灭
猜你喜欢

Construction and business practice of Zhongke brain knowledge map platform

Practice of traffic recording and playback in vivo

Analyzing more than 7million R & D needs, it is found that these eight programming languages are the most needed in the industry!

总结|机器视觉中三大坐标系及其相互关系

Yyds dry goods inventory hands-on teaching you to carry out the packaging and release of mofish Library (fishing Library)

SSM integration exception handler and project exception handling scheme

七一献礼:易鲸捷 “百日会战”完美收官 贵阳银行数据库提前封板

Another graduation season

Data security industry series Salon (III) | data security industry standard system construction theme Salon

The median salary of TSMC's global employees is about 460000, and the CEO is about 8.99 million; Apple raised the price of iPhone in Japan; VIM 9.0 releases | geek headlines
随机推荐
Bone conduction non ear Bluetooth headset brand, bone conduction Bluetooth headset brand recommendation
PyC file decompile
Understand the key technology of AGV -- the difference between laser slam and visual slam
Conditions and solutions of deadlock
SQL solves the problem of continuous login deformation holiday filtering
SSM integration exception handler and project exception handling scheme
Vscade set multi line display of tab
Classifier visual interpretation stylex: Google, MIT, etc. have found the key attributes that affect image classification
Everyone Xinfu builds: a one-stop intelligent business credit service platform
Leetcode -- number of palindromes
路由模式:hash和history模式
忆当年高考|成为程序员的你,后悔了吗?
Memory alignment of structure
绝对真理和相对真理思考
SQLServer查询哪些索引利用率低
Boot transaction usage
云原生的 CICD 框架:Tekton
头条 | 亚控科技产品入选中纺联《纺织服装行业数字化转型解决方案重点推广名录》
Multi data source configuration code
Summary | three coordinate systems in machine vision and their relationships