当前位置:网站首页>leetcode 5 最长回文子串std::pair 和 make_pair运用
leetcode 5 最长回文子串std::pair 和 make_pair运用
2022-08-08 06:21:00 【亓逸】
LeetCode 5
https://leetcode-cn.com/problems/longest-palindromic-substring/
给你一个字符串 s,找到 s 中最长的回文子串。
输入:s = “babad”
输出:“bab”
解释:“aba” 同样是符合题意的答案。
class Solution {
public:
std::pair<int, int> Palindrome(const string& s, int left, int right, const int& length){
int last_left = left, last_right = left;
while(left >= 0 && right < length)
{
if ( s[left] != s[right] )break;
last_left = left, last_right = right;
left--, right++;
}
return make_pair(last_left, last_right);
}
string longestPalindrome(string s) {
int length = s.size();
if (length == 1)return s;
int maxn = 0;
std::pair<int, int> maxStr;
for(int i = 0;i < length-1;i++){
auto [i1, j1] = Palindrome(s, i, i, length);
auto [i2, j2] = Palindrome(s, i, i+1, length);
if ( maxn < j2-i2 ){
maxn = j2-i2;
maxStr = make_pair(i2, j2);
}
if ( maxn < j1-i1 ){
maxn = j1-i1;
maxStr = make_pair(i1, j1);
}
}
return s.substr(maxStr.first, maxStr.second-maxStr.first+1);
}
};
边栏推荐
猜你喜欢
随机推荐
快要“金九银十”了,你开始准备了吗?
YoloV4训练自己的数据集(二)
C face recognition
AttributeError: ‘GridSearchCV‘ object has no attribute ‘grid_scores_‘
postgres 安装 14 版本出现错误提示解决办法
APISIX Ingress v1.5-rc1 发布
Redis 的内存策略
File Operations - IO
kdeplot()核密度估计图的介绍
独立成分分析ICA/FastICA
分页组件的使用
stack-queue
The amount of parameters and calculation of neural network, is the neural network a parametric model?
大恒工业相机搭建双目相机(软件)
docker安装Mysql和其数据持久化
Shorthand for flex layout properties
selenium模拟登录某宝
Rust学习:5_所有权与借用
MySQL5
【ESP8266】ESP12S/12F 最小系统设计及typeC自动下载电路设计









