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);
        }
    }
}
相关推荐
Warren989 分钟前
MySQL查询语句详解
java·开发语言·数据库·mysql·算法·蓝桥杯·maven
丶小鱼丶21 分钟前
Spring之【循环引用】
java·spring
hqxstudying1 小时前
Java向量化
java·开发语言
●VON1 小时前
重生之我在暑假学习微服务第七天《微服务之服务治理篇》
java·学习·微服务·云原生·nacos·架构·springcloud
你知道烟火吗2 小时前
谈谈对反射的理解?
java·开发语言·spring boot·后端
我爱996!2 小时前
Spring IoC&DI
java·后端·spring
啊阿狸不会拉杆2 小时前
《Java 程序设计》核心知识点梳理与深入探究
java·开发语言·python·算法·php·intellij-idea
盖世英雄酱581362 小时前
事务报错,为何数据还是插入成功了❓
java·数据库·后端
草莓熊Lotso2 小时前
【LeetCode刷题指南】--单值二叉树,相同的树
c语言·数据结构·算法·leetcode·刷题
it自2 小时前
Redisson在Spring Boot项目中的集成与实战
java·spring boot·redis·后端·缓存