o(n)算法地址:http://blog.163.com/zhaohai_1988/blog/static/2095100852012716105847112/

/* pivot varies when the size of substrig is even or odd
* member function parameter varies according to the size of substring
* complexity O(n*n)
*/ class Solution {
public:
string Palindrome(string s, int a,int b)
{
string tmp;
if(a<||b>=s.size())return tmp;
while(a>=&&b<s.size())
{
if(s[a]!=s[b])break;
a--;
b++;
}
tmp=s.substr(a+,b-a-);
return tmp;
}
string longestPalindrome(string s) {
if(s.size()<=)return s;
int start,end;
string maxp,tmp1,tmp2;
for (int i=;i<s.size();i++)
{ tmp1=Palindrome(s,i,i+);
tmp2=Palindrome(s,i-,i+);
if(tmp1.size()<tmp2.size())tmp1=tmp2;
if(maxp.size()<tmp1.size())maxp=tmp1;
} return maxp;
}
};

Leetcode005 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 ...

  2. leetcode--5. Longest Palindromic Substring

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

  3. [LeetCode] Longest Palindromic Substring 最长回文串

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

  4. No.005:Longest Palindromic Substring

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

  5. Leetcode Longest Palindromic Substring

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

  6. 【leedcode】 Longest Palindromic Substring

    Given a , and there exists one unique longest palindromic substring. https://leetcode.com/problems/l ...

  7. [LeetCode_5] Longest Palindromic Substring

    LeetCode: 5. Longest Palindromic Substring class Solution { public: //动态规划算法 string longestPalindrom ...

  8. 5. Longest Palindromic Substring

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

  9. leetcode-【中等题】5. Longest Palindromic Substring

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

随机推荐

  1. Codeforces Round #364 (Div. 2) Cells Not Under Attack

    Cells Not Under Attack 题意: 给出n*n的地图,有给你m个坐标,是棋子,一个棋子可以把一行一列都攻击到,在根据下面的图,就可以看出让你求阴影(即没有被攻击)的方块个数 题解: ...

  2. javascript中标签与break和continue的配合使用

    var num = 0; outermost: for (var i=0; i<10; i++) { for (var j=0; j<10; j++) { if (j==5 || i==5 ...

  3. VS2008 调试记录

    <以后追加> F10 单步调试 F11 进入函数 SHIFT+F11  推出当前函数

  4. ice介绍 z

    什么是ICE(Internet Communications Engine)呢,它是由Zeroc公司开 发的一套开源中间件系统,与DCOM,CORBA,WEB SERVICEDcom类似,支持RPC( ...

  5. ruby的hash学习笔记例: 将字符串文本中的单词存放在map中

    text = 'The rain in Spain falls mainly in the plain.'first = Hash.new []second = Hash.new {|hash,key ...

  6. Web Service 性能测试工具比较

    背景 希望选择一款Web Service性能测试工具,能真实模拟大量用户访问网站时的请求,从而获取服务器当前的请求处理能力(请求数/秒).以微信服务器为例,每个用户用独立的登录token,做各种操作, ...

  7. Maven项目中,编译proto文件成Java类

    新建Maven项目 新建一个 Maven 项目: pom定义了最小的maven2元素,即:groupId,artifactId,version. groupId:项目或者组织的唯一标志,并且配置时生成 ...

  8. flask-sqlalchemy分表解决方案

    转自:http://ju.outofmemory.cn/entry/61448 关键词: flask-sqlalchemy, sqlalchemy, 分表,分库 大型系统.海量数据肯定涉及到分库分表这 ...

  9. springMVC spring hibernate pom.xml备份

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/20 ...

  10. ormlite性能对比

    看了一下现在的android设备,性能都不差,就懒得直接用sqlite,直接上ORM框架把,上网搜了一圈,觉得androrm, ormlite 这两个不错,当然,还有点别的,这里就不多做介绍,竟然说明 ...