Created
May 21, 2020 04:07
-
-
Save wpaladins/724cb62b5c320f9aee744dfeea9a2dd9 to your computer and use it in GitHub Desktop.
5. 最长回文子串
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
string longestPalindrome(string s) { | |
int n = s.size(), jT = 0, kT = 0, maxL = 0; | |
for (int i = 0; i < n; ++i) { | |
int j = i, k = i; | |
while (j >= 0 && k < n) { | |
if (s[j] == s[k]) { | |
j--; | |
k++; | |
} | |
else { | |
break; | |
} | |
} | |
if (j + 1 != k - 1 && k - j + 1 > maxL) { | |
jT = j + 1; | |
kT = k - 1; | |
maxL = k - j + 1; | |
} | |
j = i, k = i + 1; | |
while (j >= 0 & k < n) { | |
if (s[j] == s[k]) { | |
j--; | |
k++; | |
} | |
else { | |
break; | |
} | |
} | |
if (j != k - 1 && k - j + 1 > maxL) { | |
jT = j + 1; | |
kT = k - 1; | |
maxL = k - j + 1; | |
} | |
} | |
return s.substr(jT, kT - jT + 1); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
记录下已经知的最大长度,之后小于等于这个长度的都跳过