Leetcode 385. Mini Parser

来源:互联网 发布:怎样用js做用户名验证 编辑:程序博客网 时间:2024/05/22 11:46

题目描述:

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
原创粉丝点击