You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
functionfn(str){if(!str.length)return''letres=str[0]constsetRes=(j,k)=>{if(str[j]!==str[k])returnwhile(str[j]&&str[j]===str[k]&&str[k]){res=res.length>=j-k+1 ? res : str.slice(k,j+1)j++k--}}for(leti=1,len=str.length;i<len;i++){setRes(i,i-1)setRes(i,i-2)}returnres}
> function longestPalindrome(str) {
let arr = []
let fun = function (s) {
for (let i = 0; i < s.length; i++) {
let v = s[i]
//查找 剩下字符中相同字符
let index = s.substring(i + 1, s.length).indexOf(v)
//存在
if (index > -1) {
arr.push(s.substring(i, index + 2))
}
//循环
fun(s.substring(i + 1))
}
}
fun(str)
}
Activity
sisterAn commentedon Oct 28, 2020
解法:动态规划
第 1 步:定义状态
dp[i][j]表示子串s[i..j]是否为回文子串,这里子串s[i..j]定义为左闭右闭区间,可以取到s[i]和s[j]。第 2 步:思考状态转移方程
对于一个子串而言,如果它是回文串,那么在它的首尾增加一个相同字符,它仍然是个回文串
第 3 步:初始状态:
代码实现:
复杂度分析:
leetcode
btea commentedon Oct 29, 2020
dp初始化有问题吧,第一轮循环就会报错
syc666 commentedon Jan 4, 2021
中心扩散法
yokots commentedon Jan 5, 2021
动态规划法
XW666 commentedon Jul 22, 2022