题解 | #编号子回文I#
编号子回文I
https://www.nowcoder.com/practice/db5995cd4783483f8b9f7a9e3b3a479f
class Solution { public: /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param s string字符串 * @return string字符串 */ bool isPalindrome(string s, int start, int end) { while (start < end) { if (s[start++] != s[end--]) { return false; } } return true; } string longestPalindrome(string s) { // write code here int maxLen = 1; int left = 0, right = 0; for (int i = 0; i < s.size(); i++) { for (int j = i+maxLen; j < s.size(); j++) { if (isPalindrome(s, i, j)) { if (j-i+1 > maxLen) { maxLen = j-i+1; left = i; right = j; } } } } return s.substr(left, maxLen); } };