Leetcode 385. Mini Parser

题目描述:

385. Mini Parser

  • Total Accepted: 1783
  • Total Submissions: 6708
  • Difficulty: Medium

Given a nested list of integers represented as a string, implement a parser to deserialize it.

Each element is either an integer, or a list -- whose elements may also be integers or other lists.

Note:You may assume that the string is well-formed:

  • String is non-empty.
  • String does not contain white spaces.
  • String contains only digits 0-9, [, - ,, ].

Example 1:

Given s = "324",

You should return a NestedInteger object which contains a single integer 324.

Example 2:

Given s = "[123,[456,[789]]]",

Return a NestedInteger object containing a nested list with 2 elements:

1. An integer containing value 123.
2. A nested list containing two elements:
    i.  An integer containing value 456.
    ii. A nested list with one element:
         a. An integer containing value 789.

Subscribe to see which companies asked this question

链接:https://leetcode.com/problems/mini-parser/


思路:

将例子的

"[123,[456,[789]]]"
转化成如下图的结构。


仔细看下这张图,我们可以看到这个图中,

1) 纯粹的数字,那么为叶子节点。

2)   如果是[],那么为一个链表,链表中可以包含叶子节点和链表。

这样一来我首先的思路是用递归,把这个字符串处理转换成树的广度优先遍历。再来看看用这种思路来看看这个例子的的处理过程是:

1)“[123,[456,[789]]]” //第一层为一个链表,链表中有两个节点,一个是存数字,一个是链表[456,[789]]

2)  [456,[789]] //第二层为一个链表,一个为数字为456,另外一个是链表[789]

3)  [789] //第三层为一个链表,里面有个叶子节点,为789.

看看我们的思想轨迹,明显是一个广度优先遍历一颗树的顺序,那么我们就按照广度优先递归的方法来做。那首先需要把一个待处理的字符串在同一层次上进行分割,然后将分割的子字符串在进行广度优先处理。按照这个思路实现

public class Solution {
    public static boolean StringIsInteger(String s) {
        if (s.contains("[") || s.contains("]") || s.contains(","))  {
            return false;
        }
        return true;
    }
     
    public static String[] SplitString(String s) {
        String[] result = null;
        ArrayList<String> resultAL = new ArrayList<String>();
        char[] sA = s.toCharArray();
        Stack<String> stack = new Stack<String>();
        Stack<Integer> stackIdx = new Stack<Integer>();
        int wordS = 0, wordE = 0;
        
        for (int i = 0; i < sA.length; i++) {
            if (sA[i] == '[') {
                stack.push("[");
                stackIdx.push(i);
            } else if (sA[i] == ']') {
                stack.pop();
                if (stack.isEmpty() && i > wordS) {
                    wordE = i;
                    resultAL.add(s.substring(wordS, wordE) + "]");
                    if (i + 1 < sA.length) {
                        if (sA[i + 1] == ',') {
                            i += 1;
                            wordS = i+1;
                        }
                    }
                }
            } else if (sA[i] == ',') {
                if (stack.isEmpty() && i > wordS) {
                    wordE = i;
                    resultAL.add(s.substring(wordS, wordE));
                    wordS = i + 1;
                }
            } else if (i == sA.length - 1) {
                String sub = s.substring(wordS, i+1) ;
                if (StringIsInteger(sub)) {
                    resultAL.add(sub);
                }
                
            }
        }
        result = new String[resultAL.size()];
        
        for (int i = 0; i < resultAL.size(); i++) {
            result[i] = resultAL.get(i);
        }
        return result;
    }
     
     
    // 385 mini parser
         
    public NestedInteger deserialize(String s) {
        if (StringIsInteger(s)) {
            NestedInteger ni = new NestedInteger(Integer.parseInt(s));
            return ni;
        } else {
            NestedInteger ni = new NestedInteger();
            String sub1 = s.substring(1, s.length() -1);
            if (sub1.length() == 0 ) {
                return ni;
            } else if (StringIsInteger(sub1)){
                ni.add(new NestedInteger(Integer.parseInt(sub1)));
                return ni;
            } else {
                String[] sa = SplitString(sub1);
                for (int i = 0; i < sa.length; i++) {
                    ni.add(deserialize(sa[i]));
                }
            }
            return ni;
        }
    }
}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值