385. Mini Parser

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, [, - ,, ].

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.

Solution: Stack

Example:[12, 45, [67, 89], [34, 51, [451, 251, 534]], 35]
思路: 遍歷將遇到的num加到cur list中,每當碰到 '[' ,將cur list push到stack中,并new一個cur_list繼續(xù)遍歷。當碰到']'時,pop一個list作為prev_list,將cur_list add進 prev_list中,prev_list作為cur_list繼續(xù)

Time Complexity: O(nm) Space Complexity: O(nm)
n 個 str (nested), m 長度

Solution Code:

class Solution {
    public NestedInteger deserialize(String s) {
        if (s == null || s.isEmpty())
            return null;
        
        if (s.charAt(0) != '[') // ERROR: special case
            return new NestedInteger(Integer.valueOf(s));
        
        Deque<NestedInteger> stack = new ArrayDeque<>();
        NestedInteger curr = null;
        int l = 0; // l shall point to the start of a number substring; 
                   // r shall point to the end+1 of a number substring
        
        for (int r = 0; r < s.length(); r++) {
            char ch = s.charAt(r);
            if (ch == '[') {
                if (curr != null) {
                    stack.push(curr);
                }
                curr = new NestedInteger();
                l = r + 1;
            } else if (ch == ']') {
                String num = s.substring(l, r);
                // add current num to cur list
                if (!num.isEmpty()) curr.add(new NestedInteger(Integer.valueOf(num)));
                // get previous list and 
                if (!stack.isEmpty()) {
                    NestedInteger pop = stack.pop();
                    pop.add(curr);
                    curr = pop;
                }
                l = r + 1;
            } else if (ch == ',') {
                if (s.charAt(r - 1) != ']') {  //except for the ',' after ']', becuase that has already been taken care of when ch == ']'
                    String num = s.substring(l, r);
                    curr.add(new NestedInteger(Integer.valueOf(num)));
                }
                l = r+1;
            }
        }

        return curr;
    }
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 背景 一年多以前我在知乎上答了有關(guān)LeetCode的問題, 分享了一些自己做題目的經(jīng)驗。 張土汪:刷leetcod...
    土汪閱讀 12,927評論 0 33
  • Question Given a nested list of integers represented as a...
    FlynnLWang閱讀 546評論 0 0
  • Given a nested list of integers represented as a string, ...
    Jeanz閱讀 454評論 0 0
  • 明天早上你就搭上了遠方的路程,你說不讓我去送你,是怕見到會舍不得吧。特別為你高興終于實現(xiàn)了自己的目標,前兩天還...
    遠山的樵夫閱讀 207評論 0 1
  • 這是清明時節(jié)時,寫的一首感懷詩,與親分享一一 時值祭節(jié)念雙親, 愧悔泣血淚滿襟。 兒今未盡長兄義, 暮見嚴慈總虧心。
    半夢悄開雨瀟菏閱讀 303評論 2 4

友情鏈接更多精彩內(nèi)容