【c++】leetcode5 最长回文子串

1.题目

5. 最长回文子串 - 力扣(LeetCode)

2.code

cpp 复制代码
class Solution {
public:
    string longestPalindrome(string s) {
        string res = "";
        for (auto i = 0U; i < s.length(); i++)
        {
            string s1 = palindrome(s, i, i);
            string s2 = palindrome(s, i, i+1);
            res = s1.length() > res.length() ? s1 : res;
            res = s2.length() > res.length() ? s2 : res;
        }
        return res;
    }

    string palindrome(string s, int l, int r)
    {
        while (l >= 0 && r < s.length() && s[l] == s[r])
        {
            l--;
            r++;
        }
        return s.substr(l+1, r-l-1);
    }
};
相关推荐
辞旧 lekkk5 小时前
【Qt】信号和槽
linux·开发语言·数据库·qt·学习·mysql·萌新
2zcode6 小时前
运动模糊图像复原的MATLAB仿真与优化
开发语言·matlab
袁雅倩19976 小时前
当吸尘器、筋膜枪都用上Type-C,供电方案该怎么选?浅谈PD取电芯片ECP5702的应用
c语言·开发语言·支持向量机·动态规划·推荐算法·最小二乘法·图搜索算法
Aaswk7 小时前
Java Lambda 表达式与流处理
java·开发语言·python
万邦科技Lafite7 小时前
京东item_get接口实战案例:实时商品价格监控全流程解析
java·开发语言·数据库·python·开放api·淘宝开放平台
王老师青少年编程8 小时前
csp信奥赛C++高频考点专项训练之字符串 --【子串查找】:[NOIP 2009 提高组] 潜伏者
c++·字符串·csp·高频考点·信奥赛·子串查找·潜伏者
Cyber4K8 小时前
【Python专项】进阶语法-系统资源监控与数据采集(1)
开发语言·python·php
初願致夕霞8 小时前
基于系统调用的Linux网络编程——UDP与TCP
linux·网络·c++·tcp/ip·udp
Le_ee9 小时前
ctfweb:php/php短标签/.haccess+图片马/XXE
开发语言·前端·php
yong99909 小时前
MATLAB读取高光谱图像
开发语言·matlab