java 385. Mini Parser(#)。java

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java 385. Mini Parser(#)。java相关的知识,希望对你有一定的参考价值。

/**
 * // 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 null if this NestedInteger holds a single integer
 *     public List<NestedInteger> getList();
 * }
 */
class Solution {
    public NestedInteger deserialize(String s) {
        //Stack<String>  stack = new Stack<>();
        Stack<NestedInteger> st = new Stack<>();
        int len = s.length(), i = 0;
        NestedInteger res = null; 
        while (i < len) {
            if (s.charAt(i) == ',') {
                i++;
                continue;
            } else if (s.charAt(i) == '[') {
                st.push(new NestedInteger());
                //stack.push("[");
                i++;
            } else if (s.charAt(i) == ']') {
                res = st.pop();
                if (!st.isEmpty()) {
                    NestedInteger cur = st.peek();
                    cur.add(res);
                }
                i++;
            } else {
                int start = i;
                //int sign = 1;
                // if (s.charAt(i) == '-') { // parse negative int
                //     sign = -1;
                //     i++;
                // } else if (s.charAt(i) == '+') {
                //     i++;
                // }
                while(i < len && ((s.charAt(i) >= '0' && s.charAt(i) <= '9')  || s.charAt(i) == '-' || s.charAt(i) == '-')) {
                    i++;
                }
                //stack.push(s.substring(start, i));
                if (!st.isEmpty()) {
                    NestedInteger cur = st.peek();
                    cur.add(new NestedInteger(Integer.parseInt(s.substring(start, i)))); 
                } else { // single int input "324"
                    return new NestedInteger(Integer.parseInt(s.substring(start, i)));
                }
            }
        }
        return res;
    }
}

以上是关于java 385. Mini Parser(#)。java的主要内容,如果未能解决你的问题,请参考以下文章

mini-parser

Leetcode: Mini Parser

JAVA 错误 1070 中的 PIG UDF

java 439.三元表达式Parser.java

java 439.三元表达式Parser.java

java 439.三元表达式Parser.java