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);
        }
    }
}
相关推荐
chxii16 分钟前
Spring Boot 响应给客户端的常见返回类型
java·spring boot·后端
老友@26 分钟前
一次由 PageHelper 分页污染引发的 Bug 排查实录
java·数据库·bug·mybatis·pagehelper·分页污染
AI分享猿26 分钟前
小白学规则编写:雷池 WAF 配置教程,用 Nginx 护住 WordPress 博客
java·网络·nginx
sp4244 分钟前
漫谈 Java 轻量级的模板技术:从字符串替换到复杂模板
java·后端
952361 小时前
数据结构-链表
java·数据结构·学习
喵手1 小时前
Java线程通信:多线程程序中的高效协作!
java
TDengine (老段)1 小时前
TDengine 字符串函数 CONCAT 用户手册
java·数据库·tdengine
one year.1 小时前
Linux:线程同步与互斥
java·开发语言
YDS8292 小时前
苍穹外卖 —— Spring Cache和购物车功能开发
java·spring boot·后端·spring·mybatis
苍老流年2 小时前
1. SpringBoot初始化器ApplicationContextInitializer使用与源码分析
java·spring boot·后端