2022-04-15每日一题

385. 迷你语法分析器

给定一个字符串 s 表示一个整数嵌套列表,实现一个解析它的语法分析器并返回解析的结果 NestedInteger 。

列表中的每个元素只可能是整数或整数嵌套列表

示例 1:

输入:s = “324”,
输出:324
解释:你应该返回一个 NestedInteger 对象,其中只包含整数值 324。

示例 2:

输入:s = “[123,[456,[789]]]”,
输出:[123,[456,[789]]]
解释:返回一个 NestedInteger 对象包含一个有两个元素的嵌套列表:

  1. 一个 integer 包含值 123
  2. 一个包含两个元素的嵌套列表:
    i. 一个 integer 包含值 456
    ii. 一个包含一个元素的嵌套列表
    a. 一个 integer 包含值 789

提示:

1 <= s.length <= 5 * 104
s 由数字、方括号 “[]”、负号 ‘-’ 、逗号 ','组成
用例保证 s 是可解析的 NestedInteger
输入中的所有值的范围是 [-106, 106]

思路:利用栈,首先要判断字符串是否以[开头,如果不是就直接把字符串构造出NestedInteger对象返回就行,如果是,就创建一个栈,然后创建一个空的NestedInteger对象,放到栈里,作为最外层,用两个指针left和right标记每个阶段的数字范围,left=1,right=1,然后right移动,直到碰到,或者],就表名一个阶段结束,把范围内的数字截取出来,加入到stack当前处理的层,也就是把数字转换成NestedInteger对象,加入到当前层,然后left=right+1,还要做一个额外判断是,如果是],相当于当前层处理结束,应该把这一个NestedInteger嵌套层pop出去。如果碰到[,表明要开始新的一层嵌套,于是先创建一个空的NestedInteger对象,放到当前层里,然后把空对象作为新层push进栈,之后left=right+1,循环处理,直到right跳出字符串
最后可以构建出一个NestedInteger对象,直接返回即可

/**
 * // This is the interface that allows for creating nested lists.
 * // You should not implement it, or speculate about its implementation
 * public interface NestedInteger {
 *     // Constructor initializes an empty nested list.
 *     public NestedInteger();
 *
 *     // Constructor initializes a single integer.
 *     public NestedInteger(int value);
 *
 *     // @return true if this NestedInteger holds a single integer, rather than a nested list.
 *     public boolean isInteger();
 *
 *     // @return the single integer that this NestedInteger holds, if it holds a single integer
 *     // Return null if this NestedInteger holds a nested list
 *     public Integer getInteger();
 *
 *     // Set this NestedInteger to hold a single integer.
 *     public void setInteger(int value);
 *
 *     // Set this NestedInteger to hold a nested list and adds a nested integer to it.
 *     public void add(NestedInteger ni);
 *
 *     // @return the nested list that this NestedInteger holds, if it holds a nested list
 *     // Return empty list if this NestedInteger holds a single integer
 *     public List<NestedInteger> getList();
 * }
 */
class Solution {
    public NestedInteger deserialize(String s) {
        if(!s.startsWith("[")){ //单纯给数字
            return new NestedInteger(Integer.valueOf(s));
        }

        Deque<NestedInteger> stack = new ArrayDeque<>();
        NestedInteger res = new NestedInteger();
        stack.push(res);
        int left = 1,right = 1;//定义左右指针
        while(right<s.length()){
            char ch = s.charAt(right);
            if(ch==']' || ch==','){ //表示一个阶段的结束
                if(left<right){ //把值加到新层里
                    String ss = s.substring(left,right);
                    stack.peek().add(new NestedInteger(Integer.valueOf(ss)));
                }
                left = right+1;
                if(ch==']'){
                        stack.pop();
                }
            }
            else if(ch=='['){ //创建一个新NestedInteger
                NestedInteger n = new NestedInteger();
                stack.peek().add(n);//原来NestedInteger里面开一层嵌套
                stack.push(n);//之后加值往新层加
                left=right+1;
            }
            right++;
        }
        return res;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值