标签:logs style -- lin str rom substr nbsp while
class Solution { public: string longestPalindrome(string s) { int x = -1; int l = -1; for (int i = 0; i < (int)s.size(); ++i){ int left = i; int right = i; while (left >= 0 && right < s.size() && s[left] == s[right]){ --left; ++right; } int tl = right - left - 1; if (tl > l){ l = tl; x = left+1; } left = i-1; right = i; while (left >= 0 && right < s.size() && s[left] == s[right]){ --left; ++right; } tl = right - left - 1; if (tl > l){ l = tl; x = left+1; } } if (l > -1){ return s.substr(x, l); } return ""; } };
号称还有O(n)的算法:
http://articles.leetcode.com/longest-palindromic-substring-part-ii/
后面再研究,
Leet code problem 5 Longest Palindromic Substring
标签:logs style -- lin str rom substr nbsp while
原文地址:http://www.cnblogs.com/nosaferyao/p/7468428.html