当前位置:网站首页>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);
}
};
边栏推荐
猜你喜欢
随机推荐
cnn convolutional neural network backpropagation, convolutional neural network dimension change
李沐老师 PyTorch版——线性回归 + softmax回归的简洁实现(3)
2021 mathematical modeling national competition question B
图像融合评价方式
flex布局属性简约速记
The amount of parameters and calculation of neural network, is the neural network a parametric model?
C face recognition
打开deploy和manager图形化界面打不开
np.iinfo和 np.finfo的用法
The use of Promise and the use of async/await
docker修改mysql配置文件
图像评价方法程序-PSNR
数字IC设计中为什么要避免锁存器(Latches)
nacos的配置中心
整数分块例题
状态压缩复习
探索数字安全中国方案
4G/5G频谱资源协同关键技术
pycharm 自定义标识符颜色
MySQL database









