当前位置:网站首页>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;
}
};
边栏推荐
- 理想之光不灭
- Unity使用UGUI设置一个简单多级水平方向下拉菜单(不需要代码)
- 虚假的暑假
- Memory alignment of structure
- Effectively use keywords to increase Amazon sales
- Sim2real environment configuration tutorial
- TypeScript数组乱序输出
- ROW_NUMBER()、RANK()、DENSE_RANK区别
- Analyzing more than 7million R & D needs, it is found that these eight programming languages are the most needed in the industry!
- mysql数据库mysqldump为啥没有创建数据库的语句
猜你喜欢
2022 the latest and most detailed will successfully set the background image in vscade and solve unsupported problems at the same time
台积电全球员工薪酬中位数约46万,CEO约899万;苹果上调日本的 iPhone 售价 ;Vim 9.0 发布|极客头条...
mysql min() 求某条件下最小的值出现多个结果
Memory alignment of structure
MySQL calculates the data within the longitude and latitude range
JS learning notes - operators
Maui学习之路(三)--Winui3深入探讨
The light of ideal never dies
What is Amazon keyword index? The consequences of not indexing are serious
[fluent] dart data type string type (string definition | string splicing | string API call)
随机推荐
通过两级网关设计来路由服务网格流量
云原生的 CICD 框架:Tekton
源码look me
SSM整合-异常处理器及项目异常处理方案
曆史上的今天:支付寶推出條碼支付;分時系統之父誕生;世界上第一支電視廣告...
图书管理系统(山东农业大学课程设计)
JS learning notes - process control
AWS virtual machine expansion
day4
JS learning notes - data types
Write your own CPU Chapter 11 - learning notes
Aike AI frontier promotion (2.15)
618 reprise en profondeur: la méthode gagnante de la famille Haier Zhi
七一献礼:易鲸捷 “百日会战”完美收官 贵阳银行数据库提前封板
JS learning notes - operators
By asp Net core downloads files according to the path exception
Source code look me
Storage, reading and writing of blood relationship data of Nepal Graph & Data Warehouse
Student course selection system (curriculum design of Shandong Agricultural University)
Vscade set multi line display of tab