美文网首页leetcode
38. Count and Say.go

38. Count and Say.go

作者: AnakinSun | 来源:发表于2019-03-22 13:32 被阅读0次

直接上代码

func countAndSay(n int) string {
    if n == 1 {
        return "1"
    }
    return count(countAndSay(n - 1))
}

func count(s string) string {
    c := string(s[0])
    count := 1
    res := ""
    for _, char := range s[1:] {
        if string(char) == c {
            count += 1
        } else {
            res = res + strconv.Itoa(count) + string(c)
            c = string(char)
            count = 1
        }
    }
    res = res + strconv.Itoa(count) + string(c)
    return res
}

相关文章

网友评论

    本文标题:38. Count and Say.go

    本文链接:https://www.haomeiwen.com/subject/wablvqtx.html