您的位置:首页 > 其它

LeetCode – Refresh – Longest Palindromic Substring

2015-03-20 07:33 417 查看
O(n2):

class Solution {
public:
string getP(string s, int start, int end) {
while (start >= 0 && end < s.size() && s[start] == s[end]) {
start--;
end++;
}
return s.substr(start+1, end - start - 1);
}
string longestPalindrome(string s) {
int len = s.size();
if (len < 2) return s;
string result = s.substr(0, 1);
for (int i = 0; i < len-1; i++) {
string s1 = getP(s, i, i);
if (s1.size() > result.size()) result = s1;
string s2 = getP(s, i, i+1);
if (s2.size() > result.size()) result = s2;
}
return result;
}
};


TBA (O(n))
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: