Leetcode 005 Longest Palindromic Substring
Posted zeroArn
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Leetcode 005 Longest Palindromic Substring相关的知识,希望对你有一定的参考价值。
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<0||b>=s.size())return tmp; while(a>=0&&b<s.size()) { if(s[a]!=s[b])break; a--; b++; } tmp=s.substr(a+1,b-a-1); return tmp; } string longestPalindrome(string s) { if(s.size()<=1)return s; int start,end; string maxp,tmp1,tmp2; for (int i=0;i<s.size();i++) { tmp1=Palindrome(s,i,i+1); tmp2=Palindrome(s,i-1,i+1); if(tmp1.size()<tmp2.size())tmp1=tmp2; if(maxp.size()<tmp1.size())maxp=tmp1; } return maxp; } };
以上是关于Leetcode 005 Longest Palindromic Substring的主要内容,如果未能解决你的问题,请参考以下文章
LeetCode 005 Longest Palindromic Substring - Java
LeetCode-Algorithms #005 Longest Palindromic Substring, Database #179 Consecutive Numbers