Golang | Leetcode Golang题解之第385题迷你语法分析器
题目:
题解:
func deserialize(s string) *NestedInteger {
index := 0
var dfs func() *NestedInteger
dfs = func() *NestedInteger {
ni := &NestedInteger{}
if s[index] == '[' {
index++
for s[index] != ']' {
ni.Add(*dfs())
if s[index] == ',' {
index++
}
}
index++
return ni
}
negative := s[index] == '-'
if negative {
index++
}
num := 0
for ; index < len(s) && unicode.IsDigit(rune(s[index])); index++ {
num = num*10 + int(s[index]-'0')
}
if negative {
num = -num
}
ni.SetInteger(num)
return ni
}
return dfs()
}