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. 《CEO说 像企业家一样思考》读书笔记

    伟大的CEO和街头小贩一样都有共同的思维方式,他们总是能够通过复杂的表象看到商业本质,化繁为简,抓住企业经营的根本要素:商业智慧.所谓商业智慧,即企业家最应关注的企业运营的六个关键要素:现金净流入.利 ...

  2. Vue + Element UI 实现权限管理系统 前端篇(八):管理应用状态

    使用 Vuex 管理应用状态 1. 引入背景 像先前我们是有导航菜单栏收缩和展开功能的,但是因为组件封装的原因,隐藏按钮在头部组件,而导航菜单在导航菜单组件,这样就涉及到了组件收缩状态的共享问题.收缩 ...

  3. java中this$0 this$1 this$2

    import java.lang.reflect.Field; public class Outer {//this$0 public class FirstInner {//this$1 publi ...

  4. 阿里2015校招面试回忆录(成功拿到offer)

    1. 引言 继上次“百度2015校园招聘面试题回忆录(成功拿到offer)”文章过后,大家都希望除了题目之外,最好能给出自己当时的回答情况,看看有没有什么回答技巧,这样更有参考价值. 嗯,建议的很对, ...

  5. WPF中的TextBlock隐藏边框

    TextBlock默认是有边框的,显示效果如下:有一个淡蓝色的边框围绕着 如果需要隐藏这个边框,则只需要在代码中加上以下代码即可: BorderBrush="{x:Null}" B ...

  6. java的classLoader分析与jettty的WebAppClassLoader

    classLoader,从名字就可以知道,用于加载class的东西. 我们知道在Java中,源文件是会被编译成class文件的,我们的程序的运行也是需要依赖这些编译成字节码的class文件,而这些字节 ...

  7. winDBG排错小记

    去年底,公司一个上线了近一年的系统逐渐出现访问缓慢,操作超时的问题.本人使用winDBG工具对抓下来的内存映象进行了诊断,虽最后没有查出什么原因,但在过程中也学到了不少东西,现记录如下 一. “Fai ...

  8. 【SpringBoot系列3】SpringBoot使用事务和AOP

    前言: 因为SpringBoot操作两者实在太简单了,我就放一起来写了. 正文(事务): /** * springboot中运用事务 * 真的超级方便,直接加上注解就ok了,连配置都省了 * @ret ...

  9. new~mac os 给终端命令写alias(及其他常用命令)及软连接

    配置执行顺序 优先级 配置 说明 1 /etc/profile 系统级别 —— 不推荐修改 2 /etc/paths 系统级别 —— 不推荐修改 3 ~/.profile 用户设置 4 ~/.bash ...

  10. php一些单选、复选框的默认选择方法(示例)

    转载 http://www.php.cn/php-weizijiaocheng-360029.html 一. radio和checkbox及php select默认选择的实现代码 1.radio单选框 ...