zj15939317693
Main Theme
2017-09-23 08:39

leetcode最长回文串题目:Longest Palindromic Substring

  • leetcode

以下是我的代码:
class Solution {
public:
string longestPalindrome(string s) {
int len = 0, left = 0, right = 0, StartIndex = 0;
for (int i = 0; i {
if (s[i] != s[i + 1])
{
left = right = i;
searchPalindrome(s, left, right, len, StartIndex);
}
else
{
left = i;
right = i + 1;
searchPalindrome(s, left, right, len, StartIndex);
}
}
if (len == 0)
len = s.size();
return s.substr(StartIndex, len);
}
void searchPalindrome(const string &s, int left, int right, int len, int StartIndex)
{
int step = 1;
while ((left - step) >= 0 && (right + step) {
if (s[left - step] != s[right + step])
break;
++step;
}
int wide = right - left + 2 * step - 1;
if (wide>len)
{
len = wide;
StartIndex = left - step + 1;
}
}
};

输入“babad”,输出是“babad”,而不是正确的“bab”,请问问题出在哪?

  • 点赞
  • 回答
  • 收藏
  • 复制链接分享

1条回答

为你推荐