本文主要是介绍Golang | Leetcode Golang题解之第394题字符串解码,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
题目:
题解:
var (src stringptr int
)func decodeString(s string) string {src = sptr = 0return getString()
}func getString() string {if ptr == len(src) || src[ptr] == ']' {return ""}cur := src[ptr]repTime := 1ret := ""if cur >= '0' && cur <= '9' {repTime = getDigits()ptr++str := getString()ptr++ret = strings.Repeat(str, repTime)} else if cur >= 'a' && cur <= 'z' || cur >= 'A' && cur <= 'Z' {ret = string(cur)ptr++}return ret + getString()
}func getDigits() int {ret := 0for ; src[ptr] >= '0' && src[ptr] <= '9'; ptr++ {ret = ret * 10 + int(src[ptr] - '0')}return ret
}
这篇关于Golang | Leetcode Golang题解之第394题字符串解码的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!