码迷,mamicode.com
首页 > 其他好文 > 详细

Leetcode 005 Longest Palindromic Substring

时间:2016-09-07 20:44:49      阅读:118      评论:0      收藏:0      [点我收藏+]

标签:

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

标签:

原文地址:http://www.cnblogs.com/zeroArn/p/5850709.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!