Leetcode 341. Flatten Nested List Iterator

来源:互联网 发布:淘宝如何清空收藏夹 编辑:程序博客网 时间:2024/06/11 09:52

341. Flatten Nested List Iterator

Total Accepted: 17597 Total Submissions: 49172 Difficulty: Medium

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

Hide Company Tags
 Google Facebook Twitter
Hide Tags
 Stack Design
Hide Similar Problems
 (M) Flatten 2D Vector (M) Zigzag Iterator (M) Mini Parser

思路:

自己的implementation没有用stack。用个LinkedList保存所有的数据,在初始化Iterator的时候就存好所有的数据。


7ms:

/** * // 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> {    LinkedList<Integer> buff = new LinkedList<Integer>();        public NestedIterator(List<NestedInteger> nestedList) {        helper(nestedList);    }    @Override    public Integer next() {        if(hasNext()) return buff.removeFirst();        return -1;    }    @Override    public boolean hasNext() {        return buff.size() == 0 ? false : true;    }        public void helper(List<NestedInteger> nestedList){        for(NestedInteger item : nestedList){            if(item.isInteger()) buff.add(item.getInteger());            else helper(item.getList());        }    }}/** * Your NestedIterator object will be instantiated and called as such: * NestedIterator i = new NestedIterator(nestedList); * while (i.hasNext()) v[f()] = i.next(); */

Stack版本:

从后向前压栈,然后next就是去除栈顶元素输出。因为next一定在hasNext之后调用。hasNext每次检查栈顶元素是不是Integer,是返回true;否则取出其List从后往前压栈,重复这个过程直到第一个元素是Integer。

运行时间:12ms。代码来自这里。

/** * // 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> {    Stack<NestedInteger> stack = new Stack<NestedInteger>();     public NestedIterator(List<NestedInteger> nestedList) {        if(nestedList==null)            return;         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.isEmpty()){            NestedInteger top = stack.peek();            if(top.isInteger()){                return true;            }else{                stack.pop();                for(int i=top.getList().size()-1; i>=0; i--){                    stack.push(top.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(); */


0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 /刘明珠.丿 团宠公主带拼夕夕制霸古代 宠你偏爱无度 救命!神君要我以身相许 新婚夜,老婆把我当成野男人替身 驯妖记:大圣养成指南 大佬家的小祖宗野又凶 穿越后我想和暴君HE 穿越成胖农女后我带全家逆袭了 特种女兵被摄政王金屋藏娇了 飞升以后,我竟然! 重生年代:团宠农女有空间 全家穿越后:她只想佛系种田 戾后重生 退婚后,指挥官的白月光只想摆烂 娘娘她不想宫斗 快穿之疯批宿主持美行凶 偏执病娇他说我有精神病 陆总的氪金小甜妻 将军,郡主又叒叕爬墙了! 残疾暴君的庶女妻 我在宗门种向日葵后拯救了修仙界 快穿:病娇男神又凶又粘人 戏精娘子总扮乖 我在系统圈中艰难生存 天道的疯批美人她靠算卦爆红 离婚后,偏执指挥官夜夜求关注 我靠无限物资全球基建 本道祖文成武德 全福夫人要和离 她从火光中来 裴先生每天都在撒娇求和 空间逃荒:团宠小尼姑路子野 别慌,学霸老爹和我一起穿越了 薄先生突然黏她上瘾 团宠崽崽是只桃花精 我那老板柔弱不能自理 反派小媳妇的逆袭指南 我有一个大佬群 二婚后,我在豪门扮柔弱 诸天仙神热搜:主神老婆竟是魔尊