1、题目描述

2、问题分析

计算每个字符所组成的字符串的回文子串。

3、代码

 string longestPalindrome(string s) {

         int maxL = ;
int index = ;
bool is_odd = false ;
string res ;
if( s.size() == ){
return res;
} for( int i = ; i < s.size() ; i++){
int n_odd = countLenOfPra(s ,i, i);
int n_even = countLenOfPra(s, i ,i+); if(n_odd > maxL ){
maxL = n_odd;
index = i;
is_odd = true;
}
if(n_even > maxL){
maxL = n_even;
index = i;
is_odd = false;
}
} if( is_odd ){
res = subPralid(s, index , index);
}else{
res = subPralid(s, index , index+);
}
return res;
} int countLenOfPra(string s ,int l ,int r){ while( l >= && r < s.size() && s[l] == s[r]){
l--;
r++;
}
int len = r-l+; return len;
} string subPralid(string s ,int l , int r){
string rs ;
while(l >= && r < s.size() && s[l] == s[r]){
l--;
r++;
}
l += ;
r -= ;
rs = s.substr(l,r-l+);
return rs;
}

LeetCode题解之Longest Palindromic Substring的更多相关文章

  1. leetcode题解 5. Longest Palindromic Substring

    题目: Given a string s, find the longest palindromic substring in s. You may assume that the maximum l ...

  2. 【LeetCode】5. Longest Palindromic Substring 最长回文子串

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:最长回文子串,题解,leetcode, 力扣,python ...

  3. 【一天一道LeetCode】#5 Longest Palindromic Substring

    一天一道LeetCode系列 (一)题目 Given a string S, find the longest palindromic substring in S. You may assume t ...

  4. 【LeetCode OJ】Longest Palindromic Substring

    题目链接:https://leetcode.com/problems/longest-palindromic-substring/ 题目:Given a string S, find the long ...

  5. 【LeetCode】005. Longest Palindromic Substring

    Given a string s, find the longest palindromic substring in s. You may assume that the maximum lengt ...

  6. 【LeetCode】5. Longest Palindromic Substring 最大回文子串

    题目: Given a string S, find the longest palindromic substring in S. You may assume that the maximum l ...

  7. 【leetcode】5. Longest Palindromic Substring

    题目描述: Given a string S, find the longest palindromic substring in S. You may assume that the maximum ...

  8. 《LeetBook》leetcode题解(5):Longest Palindromic [M]——回文串判断

    我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...

  9. LeetCode OJ:Longest Palindromic Substring(最长的回文字串)

    Given a string S, find the longest palindromic substring in S. You may assume that the maximum lengt ...

随机推荐

  1. 03-01:springboot 整合jsp

    1.修改pom文件,添加坐标 <!-- jstl -->        <dependency>            <groupId>javax.servlet ...

  2. Intent的那些事儿

    请原谅我用这么文艺的标题来阐释一颗无时无刻奔腾着的2B青年的心.可是今天要介绍的Intent绝不2B,甚至在我看来,或许还有些许飘逸的味道,至于飘逸在哪里呢?那我们就好好来剖析剖析Intent和它的好 ...

  3. h5实现输入框fixed定位在屏幕最底部兼容性

    1.问题由来 做h5 已经有很长一段时间了,现在做的工作h5比pc上的更多,曾经解决pc端IE各个版本的兼容性也是伤透脑筋,原以为h5的会更好,殊不知,还有更头疼的问题,当设计师要设计一个聊天窗口,把 ...

  4. tomcat配置说明,配置不用访问工程名

    # 配置项目访问不用输入项目名称 # [重要]亲测 <Host>中的 appBass="" 一定不能带目录,必须为空,因为启动tomcat会启动appBass下面的所有 ...

  5. Solidity的delete操作

    Solidity中有个特殊的操作符delete用于释放空间,因为区块链技术做为一种公用资源,为避免大家滥用.且鼓励主动对空间的回收,释放空间将会返还一些gas. delete关键字的作用是对某个类型值 ...

  6. Java NIO系列教程(三) Buffer

    Java NIO中的Buffer用于和NIO通道进行交互.如你所知,数据是从通道读入缓冲区,从缓冲区写入到通道中的.交互图如下: 缓冲区本质上是一块可以写入数据,然后可以从中读取数据的内存.这块内存被 ...

  7. 装饰器(Decorator)模式

    1  装饰模式能够实现动态的为对象添加功能,是从一个对象外部来给对象添加功能.通常给对象添加功能,要么直接修改对象添加相应的功能,要么派生对应的子类来扩展,抑或是使用对象组合的方式.显然,直接修改对应 ...

  8. office online server部署和简单操作

    office online server是Office Web Apps Server的升级版本,安装环境必须为 Windows Server 2012 R2 参考地址:https://technet ...

  9. .netcore入门

    开发环境:windows    编辑器: Visual Studio Code 环境安装: .Net Core 1.1 SDK     https://www.microsoft.com/net/co ...

  10. rsync --include-from --exclude-from的理解

    rsync --include-from --exclude-from的理解: 1.同时添加--include-from --exclude-from时.后者是对前者的结果进行排除 如:“--incl ...