class Solution {
public:
string res = "";
string MaxLength(string s) {
if (s.size() < 2)return s;
for (int i = 0; i < s.size(); i++) {
expandAroundCenter(s,i,i);
expandAroundCenter(s,i,i+1);
}
return res;
}
void expandAroundCenter(string& s,int left,int right) {
while (left >= 0 && right < s.size() && s[left] == s[right]) {
left--;
right++;
}
if (right - left - 1 > res.size()) {
res = s.substr(left + 1, right - left - 1);
}
}
};
int main() {
string s = "babad";
Solution ss;
cout << ss.MaxLength(s) << endl;
return 0;
}
2、寻找最长回文子串
最新推荐文章于 2025-08-13 19:57:41 发布