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);
        }
    }
}
相关推荐
码农颜1 分钟前
6.4.1 监听连接
java·数据库·mysql
mldong1 分钟前
会签三兄弟:并行/串行/按比例的实现与取舍
java·架构
Nil2082 分钟前
leetcode 230二叉搜索树中第k小的元素
算法·leetcode·职场和发展
旖旎夜光10 分钟前
LeetCode 69:x 的平方根(二分查找) —— 题解
数据结构·c++·算法·leetcode·二分查找
学习星球21 分钟前
【LeetCode算法题精讲】图算法精讲——从图遍历到拓扑排序
数据结构·算法·leetcode·图搜索
Java后端的Ai之路25 分钟前
01、Python - 设计模式介绍
java·人工智能·python·设计模式·通用
zzzll111126 分钟前
大模型技术原理与应用实践
java·数据库·人工智能
asdfg125896330 分钟前
java.util包中的ArrayList&&Collections的目的和用途
java·arraylist·collections
余额瞒着我当琳31 分钟前
C++STL--list底层实现,迭代器分类,模拟list的迭代器封装、实现
java·开发语言·c++
吴声子夜歌38 分钟前
Java面试——数据结构(二)
java·数据结构·面试