题解 | #最长回文子串#

最长回文子串

https://www.nowcoder.com/practice/12e081cd10ee4794a2bd70c7d68f5507

const rl = require("readline").createInterface({ input: process.stdin });

rl.on('line', function(line) {
    let str = line
    let temp = ''
    let result = ''
    for(let i=0;i<str.length;i++) {
        temp = ''
        for(let j=i;j<str.length;j++) {
            temp+=str[j]
            if(temp.split('').reverse().join('')==temp&&temp.length>result.length) {
                result = temp
            }
        }
    }
    console.log(result.length)
})

这个题的关键是1.回文子串的判断(反转前=反转后)2.从头到尾遍历字符,逐步增加字符长度去判断是否为回文子串

全部评论

相关推荐

点赞 评论 收藏
分享
投递北京经纬恒润科技股份有限公司等公司10个岗位
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务