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()
}
相关推荐
6Hzlia6 小时前
【Hot 100 刷题计划】 LeetCode 48. 旋转图像 | C++ 矩阵变换题解
c++·leetcode·矩阵
Morwit8 小时前
【力扣hot100】 1. 两数之和
数据结构·c++·算法·leetcode·职场和发展
py有趣8 小时前
力扣热门100题之岛屿的数量(DFS/BFS经典题)
leetcode·深度优先·宽度优先
qinian_ztc9 小时前
frida 14.2.18 安装报错解决
算法·leetcode·职场和发展
Wenweno0o10 小时前
Eino - 错误处理与稳定性
golang·智能体·eino
田梓燊11 小时前
2026/4/11 leetcode 3741
数据结构·算法·leetcode
王码码203511 小时前
Go语言中的Elasticsearch操作:olivere实战
后端·golang·go·接口
Tomhex11 小时前
Go语言import用法详解
golang·go
小肝一下13 小时前
每日两道力扣,day8
c++·算法·leetcode·哈希算法·hot100
Tomhex13 小时前
Golang空白导入的真正用途
golang·go