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);
        }
    }
}
相关推荐
CXDNW21 分钟前
【算法篇】哈希类(笔记)
c++·笔记·算法·leetcode·哈希算法
Bro_cat21 分钟前
归并排序(Merge Sort)
java·数据结构·算法·排序算法·归并排序
Casual_Lei25 分钟前
Delta Lake
java·hadoop
一瓢一瓢的饮 alanchan26 分钟前
【运维监控】系列文章汇总索引
java·运维·kafka·grafana·prometheus·influxdb·运维监控
忍界英雄27 分钟前
LeetCode:2848. 与车的相交点 一次遍历,时间复杂度O(n)
算法·leetcode·职场和发展
Tech Synapse36 分钟前
Java怎么把多个对象的list的数据合并
java·windows·list
武昌库里写JAVA1 小时前
玩转springboot之springboot热部署
java·c语言·开发语言·数据结构·算法
blammmp1 小时前
Java:抽象类和接口(1)
java·开发语言
customer081 小时前
【开源免费】基于SpringBoot+Vue.JS房产销售系统(JAVA毕业设计)
java·vue.js·spring boot·spring cloud·java-ee·eclipse·maven
Lenyiin2 小时前
3286、穿越网格图的安全路径
c++·算法·leetcode