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
  • Subscribe Subscribed
    • Tech Share
    • Already have a WordPress.com account? Log in now.
    • Tech Share
    • Subscribe Subscribed
    • Sign up
    • Log in
    • Report this content
    • View site in Reader
    • Manage subscriptions
    • Collapse this bar
 

Loading Comments...
 

    Design a site like this with WordPress.com
    Get started