Golang | Leetcode Golang题解之第385题迷你语法分析器

题目:

题解:

Go 复制代码
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()
}
相关推荐
漂流瓶jz20 小时前
UVA-1442 洞穴 题解答案代码 算法竞赛入门经典第二版
c++·算法·题解·aoapc·算法竞赛入门经典·uva
圣保罗的大教堂20 小时前
leetcode 835. 图像重叠 中等
leetcode
五彩小白1 天前
GO语言装饰器语法
golang
wabs6661 天前
关于二叉树【力扣572.另一棵树的子树的思考】
数据结构·c++·算法·leetcode·二叉树
hanlin031 天前
刷题笔记:力扣第41题-缺失的第一个正数
笔记·算法·leetcode
0+1111 天前
算法 --滑动窗口
c++·算法·leetcode
木井巳1 天前
【记忆化搜索】最长递增子序列
java·算法·leetcode·深度优先·剪枝·推荐算法
圣保罗的大教堂1 天前
leetcode 1401. 圆和矩形是否有重叠 中等
leetcode
金金计较.1 天前
Go语言-4
开发语言·golang
hanlin031 天前
刷题笔记:力扣第76题-最小覆盖子串
笔记·算法·leetcode