• [LeetCode] 341. 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:

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

    Example 2:

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

    扁平化嵌套列表迭代器。

    题意是给一个带有嵌套的列表,里面存的是整数。请你把它扁平化,满足当调用hasNext()函数的时候,能返回相应的数字。我这里提供两种做法,一种是递归,另一种用到stack。

    首先是递归的做法。首先创建一个空的列表res和一个index变量,记录list遍历到的位置。因为是嵌套列表,所以当遍历input列表的时候,有可能是数字,也有可能不是。如果不是数字,则继续递归调用helper函数,去处理sub list。如果是数字,则把数字加入res列表。其他的部分就比较直观了。

    时间O(n)

    空间O(n)

    Java实现

     1 public class NestedIterator implements Iterator<Integer> {
     2     List<Integer> list = new ArrayList<>(); // 普通List
     3     int index = 0;
     4 
     5     public NestedIterator(List<NestedInteger> nestedList) {
     6         // 预处理,扁平化嵌套list
     7         helper(nestedList);
     8     }
     9 
    10     public void helper(List<NestedInteger> nestedList) {
    11         // 循环List中每一个对象
    12         for (NestedInteger i : nestedList) {
    13             // 如果是数字
    14             if (i.isInteger()) {
    15                 // 将该数字加入普通List
    16                 list.add(i.getInteger());
    17             } else {
    18                 // 如果不是数字,递归子问题解决
    19                 helper(i.getList());
    20             }
    21         }
    22     }
    23 
    24     @Override
    25     public Integer next() {
    26         if (!hasNext()) {
    27             return null;
    28         }
    29         return list.get(index++);
    30     }
    31 
    32     @Override
    33     public boolean hasNext() {
    34         return index < list.size();
    35     }
    36 }
    37 
    38 /**
    39  * Your NestedIterator object will be instantiated and called as such:
    40  * NestedIterator i = new NestedIterator(nestedList);
    41  * while (i.hasNext()) v[f()] = i.next();
    42  */

    另一种做法是用到栈stack。既然用到stack了,而且stack是先进后出,所以试着从input list的尾部往头部遍历。如果遍历到的是数字integer,则把这个数字加入stack;如果遍历到的不是integer,那么在把这个list从stack里弹出的同时,也从其尾部往头部遍历,将遍历到的数字加入stack。

    时间O(n)

    空间O(n)

    Java实现

     1 public class NestedIterator implements Iterator<Integer> {
     2     Stack<NestedInteger> stack;
     3 
     4     public NestedIterator(List<NestedInteger> nestedList) {
     5         stack = new Stack<>();
     6         for (int i = nestedList.size() - 1; i >= 0; i--) {
     7             stack.push(nestedList.get(i));
     8         }
     9     }
    10 
    11     @Override
    12     public Integer next() {
    13         return stack.pop().getInteger();
    14     }
    15 
    16     @Override
    17     public boolean hasNext() {
    18         while (!stack.isEmpty()) {
    19             NestedInteger cur = stack.peek();
    20             if (cur.isInteger()) {
    21                 return true;
    22             }
    23             stack.pop();
    24             for (int i = cur.getList().size() - 1; i >= 0; i--) {
    25                 stack.push(cur.getList().get(i));
    26             }
    27         }
    28         return false;
    29     }
    30 }
    31 
    32 /**
    33  * Your NestedIterator object will be instantiated and called as such:
    34  * NestedIterator i = new NestedIterator(nestedList);
    35  * while (i.hasNext()) v[f()] = i.next();
    36  */

    LeetCode 题目总结

  • 相关阅读:
    poj3268(Silver Cow Party)最短路
    关于Phaser
    关于StampedLock
    关于AQS
    关于Exechanger
    关于Semaphore
    关于CyclicBarrier
    关于CountDownLatch
    关于BlockingQueue
    关于ThreandLocal
  • 原文地址:https://www.cnblogs.com/cnoodle/p/13559030.html
Copyright © 2020-2023  润新知