Skip to content

Tech Share

  • Home
  • Introduction
  • Contact

Golang-find the longest palindrome

Featured ~ City Rats ~ Leave a comment
func longestPalindrome(s string) string {
    maxLen := len(s)

    for l := maxLen; l > 0; l-- {
        for i := 0; i + l <= maxLen; i++ {
            lp, rp := i, i + l -1

            for lp < rp && s[lp] == s[rp] {
                lp++
                rp--
            }

            if lp >= rp {
                return s[i:i+l]
            }
        }
    }

    return s[0:1]
}

文章

Recent Posts

  • Golang-find the longest palindrome 2019-05-08
Blog at WordPress.com.
Privacy & Cookies: This site uses cookies. By continuing to use this website, you agree to their use.
To find out more, including how to control cookies, see here: Cookie Policy
  • Tech Share
    View site in Reader
    Manage subscriptionsSign upLog in
    Report this content
    Collapse this bar
Loading Comments...
Create your own website, free on WordPress.com.Create your own website.
Start free