NC17.最长回文子串
最长回文子串
https://www.nowcoder.com/practice/b4525d1d84934cf280439aeecc36f4af?tpId=196&rp=1&ru=%2Fexam%2Foj&qru=%2Fexam%2Foj&sourceUrl=%2Fexam%2Foj%3Ftab%3D%25E7%25AE%2597%25E6%25B3%2595%25E7%25AF%2587%26topicId%3D196&difficulty=&judgeStatus=&tags=&title=&gioEnter=menu
NC17.最长回文子串
class Solution {
public:
int getLongestPalindrome(string A) {
int rst = 0;
int len = (int)A.length();
for (int i = 0; i < len - rst; ++i) {
for (int j = len - 1; j >= i; --j) {
if (isPalindrome(A, i, j)) {
rst = max(rst, abs(j - i + 1));
}
}
}
return rst;
}
private:
bool isPalindrome(const string& s, int l, int r) {
while (l < r) {
if (s[l++] != s[r--]) {
return false;
}
}
return true;
}
};
解题思路:
难点1:isPalindrome
的抽象;
难点2:双指针的抽象;
难点3:len - rst
进行优化,不用遍历完所有字符,如果最后剩下的字符数量小于rst
就不用遍历了;