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

题目:

题解:

java 复制代码
class Solution {
    int index = 0;

    public NestedInteger deserialize(String s) {
        if (s.charAt(index) == '[') {
            index++;
            NestedInteger ni = new NestedInteger();
            while (s.charAt(index) != ']') {
                ni.add(deserialize(s));
                if (s.charAt(index) == ',') {
                    index++;
                }
            }
            index++;
            return ni;
        } else {
            boolean negative = false;
            if (s.charAt(index) == '-') {
                negative = true;
                index++;
            }
            int num = 0;
            while (index < s.length() && Character.isDigit(s.charAt(index))) {
                num = num * 10 + s.charAt(index) - '0';
                index++;
            }
            if (negative) {
                num *= -1;
            }
            return new NestedInteger(num);
        }
    }
}
相关推荐
DASXSDW16 小时前
NET性能优化-使用RecyclableBuffer取代RecyclableMemoryStream
java·算法·性能优化
kfepiza16 小时前
CAS (Compare and Swap) 笔记251007
java·算法
kfepiza16 小时前
Java的`volatile`关键字 笔记251007
java
风雨同舟的代码笔记16 小时前
JDK1.8 String类源码学习
java
苹果醋316 小时前
数据结构其一 线性表
java·运维·spring boot·mysql·nginx
华仔啊16 小时前
前后端防重复提交的 6 种落地实现:从按钮禁用到 AOP 全自动防护
java·后端
墨染点香16 小时前
LeetCode 刷题【103. 二叉树的锯齿形层序遍历、104. 二叉树的最大深度、105. 从前序与中序遍历序列构造二叉树】
算法·leetcode·职场和发展
lllsure16 小时前
Java Stream API
java·开发语言
chirrupy_hamal16 小时前
IO 流篇
java
Le1Yu17 小时前
2025-10-6学习笔记
java·笔记·学习