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()
}
相关推荐
福大大架构师每日一题14 小时前
ollama v0.30.7 正式发布:Hermes 桌面端落地,接口、文档、底层依赖全方位优化
golang·log4j
不爱编程的小陈16 小时前
深入解析 Go 网络 I/O 的底层引擎:从 epoll 到 netpoll
服务器·网络·golang
人道领域17 小时前
【LeetCode刷题日记】47.全排列Ⅱ
java·开发语言·算法·leetcode
Navigator_Z17 小时前
LeetCode //C - 1095. Find in Mountain Array
c语言·算法·leetcode
何以解忧,唯有..20 小时前
Go 语言数据类型详解:从基础到复合类型
开发语言·golang·mfc
踏着七彩祥云的小丑20 小时前
Go学习第7天:Map集合 + 递归函数 + 类型转换
开发语言·学习·golang·go
何以解忧,唯有..20 小时前
Go语言变量的声明方式详解
开发语言·后端·golang
寂夜了无痕20 小时前
Go 多版本管理工具G 保姆级安装配置教程
golang·go多版本管理
张忠琳21 小时前
【Go 1.26.4】Golang Slice 深度解析
开发语言·后端·golang
暖阳华笺1 天前
【数据结构与算法】哈希专题
数据结构·c++·算法·leetcode·哈希算法