Leetcode-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].

Explain

這道題最核心的意思就是把一個可能會嵌套很多層的數(shù)組扁平化习瑰。那無疑是得使用遞歸的猾编。我們從頭到尾遍歷一次,如果是數(shù)字就push進vector虱疏,如果是一個數(shù)組就調(diào)用遞歸函數(shù),等待這個數(shù)組扁平化后钾菊,再將數(shù)組內(nèi)的每一項push進vector里面慢哈。除此之外呢, 這道題會有幾個極端的例子绊汹,就是數(shù)組為空磺箕,數(shù)組嵌套的數(shù)組為空的情況奖慌,注意判斷就好。那么下面就是實現(xiàn):

Code

/**
 * // This is the interface that allows for creating nested lists.
 * // You should not implement it, or speculate about its implementation
 * class NestedInteger {
 *   public:
 *     // Return true if this NestedInteger holds a single integer, rather than a nested list.
 *     bool isInteger() const;
 *
 *     // Return the single integer that this NestedInteger holds, if it holds a single integer
 *     // The result is undefined if this NestedInteger holds a nested list
 *     int getInteger() const;
 *
 *     // Return the nested list that this NestedInteger holds, if it holds a nested list
 *     // The result is undefined if this NestedInteger holds a single integer
 *     const vector<NestedInteger> &getList() const;
 * };
 */
class NestedIterator {
public:
    NestedIterator(vector<NestedInteger> &nestedList) {
        res = helper(nestedList);
    }

    int next() {
        pos++;
        return res[pos - 1];
    }

    bool hasNext() {
        
        return !res.empty() && res.size() - 1 >= pos;
    }
private:
    vector<int> res;
    int pos = 0;
    
    
    vector<int> helper(vector<NestedInteger> &nestedList) {
        vector<int> res;
        if (nestedList.empty()) return res;
        for (int i = 0; i < nestedList.size(); i++) {
            if (nestedList[i].isInteger()) {
                res.push_back(nestedList[i].getInteger());
            } else {
                vector<int> cur = helper(nestedList[i].getList());
                for (auto c : cur) {
                    res.push_back(c);
                }
            }
        }
        return res;
    }
};

/**
 * Your NestedIterator object will be instantiated and called as such:
 * NestedIterator i(nestedList);
 * while (i.hasNext()) cout << i.next();
 */

?著作權歸作者所有,轉載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末松靡,一起剝皮案震驚了整個濱河市简僧,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌雕欺,老刑警劉巖岛马,帶你破解...
    沈念sama閱讀 221,406評論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異屠列,居然都是意外死亡啦逆,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,395評論 3 398
  • 文/潘曉璐 我一進店門笛洛,熙熙樓的掌柜王于貴愁眉苦臉地迎上來夏志,“玉大人,你說我怎么就攤上這事苛让」得铮” “怎么了?”我有些...
    開封第一講書人閱讀 167,815評論 0 360
  • 文/不壞的土叔 我叫張陵狱杰,是天一觀的道長瘦材。 經(jīng)常有香客問我,道長仿畸,這世上最難降的妖魔是什么食棕? 我笑而不...
    開封第一講書人閱讀 59,537評論 1 296
  • 正文 為了忘掉前任,我火速辦了婚禮错沽,結果婚禮上宣蠕,老公的妹妹穿的比我還像新娘。我一直安慰自己甥捺,他們只是感情好,可當我...
    茶點故事閱讀 68,536評論 6 397
  • 文/花漫 我一把揭開白布镀层。 她就那樣靜靜地躺著镰禾,像睡著了一般。 火紅的嫁衣襯著肌膚如雪唱逢。 梳的紋絲不亂的頭發(fā)上吴侦,一...
    開封第一講書人閱讀 52,184評論 1 308
  • 那天,我揣著相機與錄音坞古,去河邊找鬼备韧。 笑死,一個胖子當著我的面吹牛痪枫,可吹牛的內(nèi)容都是我干的织堂。 我是一名探鬼主播叠艳,決...
    沈念sama閱讀 40,776評論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼易阳!你這毒婦竟也來了附较?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 39,668評論 0 276
  • 序言:老撾萬榮一對情侶失蹤潦俺,失蹤者是張志新(化名)和其女友劉穎拒课,沒想到半個月后,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體事示,經(jīng)...
    沈念sama閱讀 46,212評論 1 319
  • 正文 獨居荒郊野嶺守林人離奇死亡早像,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,299評論 3 340
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了肖爵。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片卢鹦。...
    茶點故事閱讀 40,438評論 1 352
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖遏匆,靈堂內(nèi)的尸體忽然破棺而出法挨,到底是詐尸還是另有隱情,我是刑警寧澤幅聘,帶...
    沈念sama閱讀 36,128評論 5 349
  • 正文 年R本政府宣布凡纳,位于F島的核電站,受9級特大地震影響帝蒿,放射性物質(zhì)發(fā)生泄漏荐糜。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,807評論 3 333
  • 文/蒙蒙 一葛超、第九天 我趴在偏房一處隱蔽的房頂上張望暴氏。 院中可真熱鬧,春花似錦绣张、人聲如沸答渔。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,279評論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽沼撕。三九已至,卻和暖如春芜飘,著一層夾襖步出監(jiān)牢的瞬間务豺,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,395評論 1 272
  • 我被黑心中介騙來泰國打工嗦明, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留笼沥,地道東北人。 一個月前我還...
    沈念sama閱讀 48,827評論 3 376
  • 正文 我出身青樓,卻偏偏與公主長得像奔浅,于是被迫代替她去往敵國和親馆纳。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 45,446評論 2 359

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