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()
}
相关推荐
serendipity_hky4 小时前
【go语言 | 第2篇】Go变量声明 + 常用数据类型的使用
开发语言·后端·golang
月明长歌5 小时前
【码道初阶】【LeetCode 110】平衡二叉树:如何用一个“Magic Number”将复杂度从O(N²)降为 O(N)?
linux·算法·leetcode
yaoh.wang5 小时前
力扣(LeetCode) 14: 最长公共前缀 - 解法思路
python·程序人生·算法·leetcode·面试·职场和发展·跳槽
周杰伦_Jay6 小时前
【Eino框架】Go语言驱动的LLM应用开发新范式
开发语言·后端·golang
埃伊蟹黄面7 小时前
算法 --- hash
数据结构·c++·算法·leetcode
ywwwwwwv8 小时前
力扣139
算法·leetcode·职场和发展
黛色正浓8 小时前
leetCode-热题100-哈希合集(JavaScript)
javascript·leetcode·哈希算法
2501_941982058 小时前
Go 进阶:发送文件/图片消息的流程与实现
开发语言·后端·golang
smj2302_796826528 小时前
解决leetcode第3777题使子字符串变交替的最少删除次数
python·算法·leetcode
Tisfy8 小时前
LeetCode 2110.股票平滑下跌阶段的数目:数学(一次遍历)
数学·算法·leetcode·题解