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);
        }
    }
}
相关推荐
cpp_25011 分钟前
P2347 [NOIP 1996 提高组] 砝码称重
数据结构·c++·算法·题解·洛谷·noip·背包dp
亦暖筑序25 分钟前
Spring AI Alibaba 报错合集:我踩过的那些坑
java·后端
indexsunny1 小时前
互联网大厂Java面试实战:核心技术与微服务架构在电商场景中的应用
java·spring boot·redis·kafka·maven·spring security·microservices
摇滚侠1 小时前
Java 多线程基础 Java Multithreading Basics
java
今天你TLE了吗1 小时前
LLM到Agent&RAG——AI概念概述 第一章:大模型
java·人工智能·语言模型·大模型
你的牧游哥1 小时前
Java 核心概念详解
java·开发语言
加农炮手Jinx1 小时前
LeetCode 146. LRU Cache 题解
算法·leetcode·力扣
加农炮手Jinx1 小时前
LeetCode 128. Longest Consecutive Sequence 题解
算法·leetcode·力扣
旖-旎1 小时前
递归(汉诺塔问题)(1)
c++·学习·算法·leetcode·深度优先·递归
深邃-1 小时前
【数据结构与算法】-顺序表链表经典算法
java·c语言·数据结构·c++·算法·链表·html5