题目
给你一个字符串 s,找到 s 中最长的回文子串
思路
循环然后找出最长的回文串然后保存
function isPalindrome(s: string): boolean {
let len = s.length;
for(let i = 0; i < len/2; i++) {
if(s.charAt(i) !== s.charAt(len- i - 1)) {
return false
}
}
return true;
}
function longestPalindrome(s: string): string {
let ans = '';
let max = 0;
let len = s.length;
for(let i = 0; i < len; i++) {
for(let j = i + 1; j <= len; j++) {
let test = s.substring(i, j)
if(isPalindrome(test) && test.length > max) {
ans = s.substring(i, j)
max = Math.max(max, ans.length)
}
}
}
return ans
};