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. SpringSecurity学习之自定义过滤器

    我们系统中的认证场景通常比较复杂,比如说用户被锁定无法登录,限制登录IP等.而SpringSecuriy最基本的是基于用户与密码的形式进行认证,由此可知它的一套验证规范根本无法满足业务需要,因此扩展势 ...

  2. Maven 的基本配置与使用

    什么是Maven Maven是基于项目对象模型(POM),可以通过一小段描述信息来管理项目的构建,报告和文档的软件项目管理工具. 发文时,绝大多数开发人员都把 Ant 当作 Java 编程项目的标准构 ...

  3. IdentityServer-Setup and Overview

    设置和概述 有两种方式创建一个IdentityServer 项目: 从零开始 使用Visual Studio的ASP.NET Identity模板 如果是从零开始,我们提供一序列的帮助及内存存储,所以 ...

  4. 《CEO说 像企业家一样思考》读书笔记

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

  5. 常用的Maven 插件

    Maven本质上是一个插件框架,它的核心并不执行任何具体的构建任务,所有这些任务都交给插件来完成. 例如编译源代码是由maven- compiler-plugin完成的.进一步说,每个任务对应了一个插 ...

  6. Solidity字符串类型

    字符串可以通过""或者''来表示字符串的值,Solidity中的string字符串不像C语言一样以\0结束,比如abcd这个字符串的长度就为我们所看见的字母的个数,它的长度为4. ...

  7. Ubuntu16.04安装mac主题(转载)

    Ubuntu16.04配置Mac主题 作者:tongqingliu 转载请注明出处:http://www.cnblogs.com/liutongqing/p/7072878.html 觉得有帮助?欢迎 ...

  8. 自定义MVC的Helper扩展方法

    记得在开发ASP.NET时候,也经常性使用C#可以写自己义的扩展方法,如:http://www.cnblogs.com/insus/p/3154363.html 或http://www.cnblogs ...

  9. 关于ASPxComboBox通过ClientInstanceName,js获取不到控件的问题

    今天突然遇到一个很奇葩的问题 ASPxComboBox中设置了ClientInstanceName.但是通过cmbOrganization.GetValue()获取不到值. 报错cmbOrganiza ...

  10. WPF TreeView 选择事件执行两次,获取TreeView的父节点的解决方法

    1.TreeView选择事件执行两次 Very often, we need to execute some code in SelectedItemChanged depending on the ...