编程知识 cdmana.com

Leetcode day 18

5. Longest text substring

var longestPalindrome = function(s) {
    if (s.length < 2) return s   //  The length is less than 2 Go straight back to s
    let l = 0, r = 0
    for (let i = 0; i < s.length; i++) {
        palindrome(i, i)
        palindrome(i, i+1)
    }

    function palindrome(n, m) {
        while (n >= 0 && m < s.length && s[n] === s[m]) {  //  Left border right border 
            n--
            m++
        }
        if (m - n > r - l) {
            r = m
            l = n
        }
    }

    return s.slice(l+1, r)
}
 Copy code 

版权声明
本文为[onepiece1205]所创,转载请带上原文链接,感谢
https://cdmana.com/2021/09/20210909122358696g.html

Tags leetcode day
Scroll to Top