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. Mac 下安装.NET Core 与 CLI

    .NET Foundation:https://github.com/dotnet/home .NET Core:https://github.com/dotnet/coreclr CLI:https ...

  2. 【JAVA】判断当前日期是否在时间点内

    public static boolean isInDate(Date date, String strDateBegin, String strDateEnd) { SimpleDateFormat ...

  3. ASP.NET MVC HtmlHelper 类的扩展方法

    再ASP.NET MVC编程中用到了R语法,在View页面编辑HTML标签的时候,ASP.NET MVC 为我们准备好了可以辅助我们写这些标签的办法,它们就是HtmlHelper.微软官方地址是:ht ...

  4. ExecutorService接口概要

    ExecutorService接口继承于Executor接口,主要提供以下额外功能: 管理终结 产生Future对象,用于跟踪一个或多个任务的进度.   ExecutorService可以被shut ...

  5. linq中last或者lastordefault不存在的问题

    在使用linq访问数据库的时候发现first以及firstordefault都存在,但是last以及lastordefault不存在.上网找寻一番发现是last只在linq to object中实现了 ...

  6. C++ STL 学习

    /* algorithm-算法 */ .copy() //此函数用在vector中只做拷贝使用,它不能让vector有自动扩充作用.如果vector的容量小于它拷贝的数据量将会报错. /* itera ...

  7. 递推、数位DP解析(以HDU 2089 和 HDU 3555 为例)

    HDU 2089 不要62 题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=2089 Problem Description 杭州人称那些傻乎乎粘嗒嗒的人 ...

  8. LINUX 下Jexus部署ASP.NET Core WebApi

    服务器:LINUX  ubuntu16.04  开发软件:VS2015 Update3   dotnet sdk: DotNetCore.1.0.0-VS2015Tools.Preview2   1. ...

  9. 设计模式之工厂方法(FactoryMethod)模式

    在五大设计原则的基础上经过GOF(四人组)的总结,得出了23种经典设计模式,其中分为三大类:创建型(5种).结构型(7种).行为型(11种).今天对创建型中的工厂方法(FactoryMethod)模式 ...

  10. 【模板】堆优化 + dij +pair 存储

    就是短 感谢Cptraserdalao的博客 #include<bits/stdc++.h> using namespace std; struct node { int val,num; ...