class Solution {
public:
string longestPalindrome(string s) {
vector<int> range = { 0,0 };
for (int i = 0; i < s.size(); i++) {
i = function(range, s, i);
}
return s.substr(range[0], range[1] - range[0] + 1);
}
int function(vector<int>& range, string s, int i) {
int low = i;
int high = low + 1;
while (high < s.size() && s[high] == s[low]) {
high++;
}
int ans = high - 1;
low--;
while (low >= 0 && high < s.size()) {
if (s[low] == s[high]) {
low--;
high++;
}
else {
break;
}
}
low++;
high--;
if (high - low > range[1] - range[0]) {
range[0] = low;
range[1] = high;
}
return ans;
}
};
【中心点向俩段遍历】【数组】leetcode.5.最长回文子串
最新推荐文章于 2022-09-13 14:56:04 发布