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()
}
相关推荐
__AtYou__2 小时前
Golang | Leetcode Golang题解之第405题数字转换为十六进制数
leetcode·golang·题解
吃着火锅x唱着歌3 小时前
Go语言设计与实现 学习笔记 第七章 内存管理(1)
笔记·学习·golang
程序猿练习生4 小时前
C++速通LeetCode简单第16题-买卖股票的最佳时机
开发语言·c++·leetcode
Chase-Hart4 小时前
【每日一题】LeetCode 1184.公交站间的距离问题(数组)
java·算法·leetcode·eclipse·intellij-idea
Chase-Hart5 小时前
【每日一题】LeetCode 815.公交路线(广度优先搜索、数组、哈希表)
数据结构·算法·leetcode·散列表·宽度优先
GZK.6 小时前
【Leetcode】70. 爬楼梯
算法·leetcode·动态规划
GoppViper7 小时前
golang学习笔记24——golang微服务中配置管理问题的深度剖析
笔记·后端·学习·微服务·golang·配置管理
景天科技苑7 小时前
【Go】Go语言中延迟函数、函数数据的类型、匿名函数、闭包等高阶函数用法与应用实战
后端·golang·回调函数·defer·匿名函数·闭包·go函数数据类型
.普通人9 小时前
c语言--力扣简单题目(链表的中间节点)讲解
c语言·leetcode·链表
蒙娜丽宁9 小时前
Go语言错误处理详解
ios·golang·go·xcode·go1.19