LeetCode 341. Flatten Nested List Iterator(嵌套列表迭代器)

来源:互联网 发布:如何检查网络是否丢包 编辑:程序博客网 时间:2024/06/05 19:20

原题网址:https://leetcode.com/problems/flatten-nested-list-iterator/

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

方法:使用栈来保持当前状态和嵌套关系。

/** * // 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<Cursor> stack = new Stack<>();    public NestedIterator(List<NestedInteger> nestedList) {        if (nestedList != null) stack.push(new Cursor(nestedList));    }    @Override    public Integer next() {        while (!stack.isEmpty()) {            Cursor cursor = stack.peek();            if (cursor.i < cursor.list.size()) {                NestedInteger nested = cursor.list.get(cursor.i++);                if (nested.isInteger()) return nested.getInteger();                stack.push(new Cursor(nested.getList()));            } else {                stack.pop();            }        }        return null;    }    @Override    public boolean hasNext() {        while (!stack.isEmpty()) {            Cursor cursor = stack.peek();            if (cursor.i < cursor.list.size()) {                NestedInteger nested = cursor.list.get(cursor.i);                if (nested.isInteger()) return true;                cursor.i ++;                stack.push(new Cursor(nested.getList()));            } else {                stack.pop();            }        }        return false;    }}/** * Your NestedIterator object will be instantiated and called as such: * NestedIterator i = new NestedIterator(nestedList); * while (i.hasNext()) v[f()] = i.next(); */class Cursor {    List<NestedInteger> list;    int i;    Cursor(List<NestedInteger> list) {        this.list = list;    }}

另一种实现方式:

/** * // 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<State> stack = new Stack<>();        public NestedIterator(List<NestedInteger> nestedList) {        stack.push(new State(nestedList));    }    @Override    public Integer next() {        hasNext();        State state = stack.peek();        return state.nestedList.get(state.pos++).getInteger();    }    @Override    public boolean hasNext() {        while (!stack.isEmpty()) {            State state = stack.peek();            if (state.pos < state.nestedList.size()) {                if (state.nestedList.get(state.pos).isInteger()) return true;                stack.push(new State(state.nestedList.get(state.pos++).getList()));            } else {                stack.pop();            }        }        return false;    }}class State {    List<NestedInteger> nestedList;    int pos;    State(List<NestedInteger> nestedList) {        this.nestedList = nestedList;    }}/** * Your NestedIterator object will be instantiated and called as such: * NestedIterator i = new NestedIterator(nestedList); * while (i.hasNext()) v[f()] = i.next(); */


0 0
原创粉丝点击