longestPalindrome

func longestPalindrome(s string) string {

    var list []rune

    for _,v := range s{
        list = append(list,v)
    }

    var res [][]int
    for k,_ := range list{

        startKey := k
        endKey := k
        for i:=0;;i++ {
            if k - i >= 0 && k + i < len(list) {
                if list[k - i] == list[k + i] {
                    startKey = k - i
                    endKey = k + i
                    continue
                }
            }
            break
        }

        tmp := []int{startKey,endKey}
        res = append(res,tmp)
        startKey2 := k
        endKey2 := k
        for i:=0;;i++ {
            if k - i >= 0 && k + i+1 < len(list) {
                if list[k - i] == list[k + i+1] {
                    startKey2 = k - i
                    endKey2 = k + i+1
                    continue
                }
            }
            break
        }
        tmp = []int{startKey2,endKey2}
        res = append(res,tmp)

    }
    max := 1
    var maxRes string
    for _,v := range res {
        if len(maxRes) == 0{
            maxRes = string(list[v[0]:v[1]+1])
        }
        if v[1]-v[0]+1>max{

            max = v[1]-v[0]+1
            maxRes = string(list[v[0]:v[1]+1])
        }
    }
    return maxRes

}

你可能感兴趣的:(longestPalindrome)