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. c++ std::sort函数调用经常出现的invalidate operator<错误原因以及解决方法

    在c++编程中使用sort函数,自定义一个数据结构并进行排序时新手经常会碰到这种错误. 这是为什么呢?原因在于什么?如何解决? 看下面一个例子: int main(int, char*[]) { st ...

  2. 纯c++实现之滚动窗口

    别在MFC了,先分析下,上图 我们以左上角为坐标原点,用position_width和position_height来保存当前显示坐标. 根据msdn说明,滚动条默认情况下的值在0~100之间. 根据 ...

  3. Scala第四章学习笔记(面向对象编程)

    延迟构造 DelayedInit特质是为编译器提供的标记性的特质.整个构造器被包装成一个函数并传递给delayedInit方法. trait DelayedInit { def deayedInit( ...

  4. NeHe OpenGL教程 第二十六课:反射

    转自[翻译]NeHe OpenGL 教程 前言 声明,此 NeHe OpenGL教程系列文章由51博客yarin翻译(2010-08-19),本博客为转载并稍加整理与修改.对NeHe的OpenGL管线 ...

  5. python (16) 如何在linux下安装lxml(pip安装,ubuntu下,centos下)

    首先需要明白lxml包依赖其他包,必须先安装其他包然后再安装lxml 安装python-pip:方便安装python的第三方包 [root@******/]# wget https://bootstr ...

  6. Django 的 CSRF 保护机制

    转自:http://www.cnblogs.com/lins05/archive/2012/12/02/2797996.html 用 django 有多久,我跟 csrf 这个概念打交道就有久了. 每 ...

  7. 算法库:blas, lapack, cblas, clapack, armadillo, openblas, mkl关系

    关于blas的介绍介绍见:http://www.cnblogs.com/dzyBK/p/4983953.html blas:提供向量和矩阵的基本运算,用fortran编写. lapack:提供向量和矩 ...

  8. lower_bound 和 upper_bound

    Return iterator to lower bound Returns an iterator pointing to the first element in the range [first ...

  9. Unable to open serial port /dev/ttyUSB0

    ubuntu12.04使用USB转串口时出现权限不够问题,如下 Unable to open serial port /dev/ttyUSB0 权限不够 解决办法: 通过增加udev规则来实现.步骤如 ...

  10. FileSystem实例化过程

    HDFS案例代码 Configuration configuration = new Configuration(); FileSystem fileSystem = FileSystem.get(n ...