LeetCode地址
算法难度
中等
JS代码
/**
* @param {string} s
* @return {string}
*/
var longestPalindrome = function(s) {
let left = 0
let index = 0
let right = 0
let max = 0
let start = 0
let len = s.length
while(index < len) {
left = index - 1
right = index
while(s[right] === s[index]) {
right++
}
index = right
while(left >= 0 && right < len && s[right] === s[left]) {
right++
left--
}
if (right - left - 1 > max) {
max = right - left - 1
start = left + 1
}
}
return s.substr(start, max)
};