341. Flatten Nested List Iterator

Description

Given a nested list of integers, implement an iterator to flatten it.

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

Example 1:
Given the list [[1,1],2,[1,1]],

By calling next repeatedly until hasNext returns false, the order of elements returned by next should be: [1,1,2,1,1].

Example 2:
Given the list [1,[4,[6]]],

By calling next repeatedly until hasNext returns false, the order of elements returned by next should be: [1,4,6].

Solution

Stack, hasNext time O(n), next time O(1), space O(n)

用stack即可實現(xiàn)牍颈。原本寫的solution是在next中處理stack內(nèi)元素涛菠,hasNext只判斷stack是否為空絮供,但這會有問題驻民。

考慮test case: [[]

壓棧之后束凑,棧不為空,hasNext返回true,但實際上next并沒有值。這說明我們需要在hasNext中處理棧內(nèi)元素离唐,才能handle這種情況。

/**
 * // This is the interface that allows for creating nested lists.
 * // You should not implement it, or speculate about its implementation
 * public interface NestedInteger {
 *
 *     // @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();
 *
 *     // @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();
 * }
 */
public class NestedIterator implements Iterator<Integer> {
    private Stack<NestedInteger> stack;
    
    public NestedIterator(List<NestedInteger> nestedList) {
        stack = new Stack<>();
        
        for (int i = nestedList.size() - 1; i >= 0; --i) {
            stack.push(nestedList.get(i));
        }
    }

    @Override
    public Integer next() {
        return stack.pop().getInteger();
    }

    @Override
    public boolean hasNext() {
        while (!stack.empty()) {
            if (stack.peek().isInteger()) {
                return true;
            }
            
            NestedInteger nested = stack.pop();

            for (int i = nested.getList().size() - 1; i >= 0; --i) {
                stack.push(nested.getList().get(i));
            }
        }
        
        return false;
    }
}

/**
 * Your NestedIterator object will be instantiated and called as such:
 * NestedIterator i = new NestedIterator(nestedList);
 * while (i.hasNext()) v[f()] = i.next();
 */
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末问窃,一起剝皮案震驚了整個濱河市亥鬓,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌域庇,老刑警劉巖嵌戈,帶你破解...
    沈念sama閱讀 219,427評論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異听皿,居然都是意外死亡熟呛,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,551評論 3 395
  • 文/潘曉璐 我一進(jìn)店門写穴,熙熙樓的掌柜王于貴愁眉苦臉地迎上來惰拱,“玉大人,你說我怎么就攤上這事啊送〕ザ蹋” “怎么了?”我有些...
    開封第一講書人閱讀 165,747評論 0 356
  • 文/不壞的土叔 我叫張陵馋没,是天一觀的道長昔逗。 經(jīng)常有香客問我,道長篷朵,這世上最難降的妖魔是什么勾怒? 我笑而不...
    開封第一講書人閱讀 58,939評論 1 295
  • 正文 為了忘掉前任,我火速辦了婚禮声旺,結(jié)果婚禮上笔链,老公的妹妹穿的比我還像新娘。我一直安慰自己腮猖,他們只是感情好鉴扫,可當(dāng)我...
    茶點故事閱讀 67,955評論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著澈缺,像睡著了一般坪创。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上姐赡,一...
    開封第一講書人閱讀 51,737評論 1 305
  • 那天莱预,我揣著相機與錄音,去河邊找鬼项滑。 笑死依沮,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播悉抵,決...
    沈念sama閱讀 40,448評論 3 420
  • 文/蒼蘭香墨 我猛地睜開眼肩狂,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了姥饰?” 一聲冷哼從身側(cè)響起傻谁,我...
    開封第一講書人閱讀 39,352評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎列粪,沒想到半個月后审磁,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,834評論 1 317
  • 正文 獨居荒郊野嶺守林人離奇死亡岂座,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,992評論 3 338
  • 正文 我和宋清朗相戀三年态蒂,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片费什。...
    茶點故事閱讀 40,133評論 1 351
  • 序言:一個原本活蹦亂跳的男人離奇死亡钾恢,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出鸳址,到底是詐尸還是另有隱情瘩蚪,我是刑警寧澤,帶...
    沈念sama閱讀 35,815評論 5 346
  • 正文 年R本政府宣布稿黍,位于F島的核電站疹瘦,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏巡球。R本人自食惡果不足惜言沐,卻給世界環(huán)境...
    茶點故事閱讀 41,477評論 3 331
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望酣栈。 院中可真熱鬧险胰,春花似錦、人聲如沸矿筝。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,022評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽跋涣。三九已至,卻和暖如春鸟悴,著一層夾襖步出監(jiān)牢的瞬間陈辱,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,147評論 1 272
  • 我被黑心中介騙來泰國打工细诸, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留沛贪,地道東北人。 一個月前我還...
    沈念sama閱讀 48,398評論 3 373
  • 正文 我出身青樓,卻偏偏與公主長得像利赋,于是被迫代替她去往敵國和親水评。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 45,077評論 2 355

推薦閱讀更多精彩內(nèi)容