for(let i = 0; i < arr.length; i++){ const a = oddCode(i, arr); const b = evenCode(i, arr); const res = [] res.push(Math.max(a, b)); }
// 任意取一个字符, 判断以它为中心的奇数回文的长度 functionoddCode(index, arr){ let left = index - 1; let right = index + 1; let count = 1; while(left >= 0 && right < arr.length){ if(arr[left] === arr[right]){ count += 2; left--; right++; }else{ return count; } } return count; }
// 任意取一个字符, 判断它是不是偶数回文串的长度 functionevenCode(index, arr){ let left = index; let right = index + 1; let count = 0; while(left >= 0 && right < arr.length){ if(arr[left] === arr[right]){ count += 2; left --; right ++; }else{ return count; } } return count; }