给你一个字符串 s,找到 s 中最长的回文子串。
示例 1:
输入:s = "babad"
输出:"bab"
解释:"aba" 同样是符合题意的答案。
示例 2:
输入:s = "cbbd"
输出:"bb"
提示:
1 <= s.length <= 1000
s 仅由数字和英文字母组成
1 /** 2 * @param {string} s 3 * @return {string} 4 */ 5 var longestPalindrome = function(s) { 6 if (s.length < 2) { 7 return s; 8 } 9 let start = 0; 10 let maxLength = 1; 11 12 function expandAroundCenter(left, right) { 13 while (left >= 0 && right < s.length && s[left] === s[right]) { 14 if (right - left + 1 > maxLength) { 15 maxLength = right - left + 1; 16 start = left; 17 } 18 left--; 19 right++; 20 } 21 } 22 for (let i = 0; i < s.length; i++) { 23 expandAroundCenter(i - 1, i + 1); 24 expandAroundCenter(i, i + 1); 25 } 26 return s.substring(start, start + maxLength); 27 };
标签:子串,expandAroundCenter,right,return,start,maxLength,回文,最长,left From: https://www.cnblogs.com/icyyyy/p/16820144.html