zoukankan      html  css  js  c++  java
  • LeetCode-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].

    Analysis:

    We use recursive iterator. If the next element is a list, we create another iterator to traverse it. One key point here is: either there is next element or the hasNext() function returns false.

    After fetch the curent element (return of Next()), we need to move the pointer to the next element. When moving, we need to (1) determine whether there is next element; (2) skip all empty lists.

    Solution:

     1 /**
     2  * // This is the interface that allows for creating nested lists.
     3  * // You should not implement it, or speculate about its implementation
     4  * public interface NestedInteger {
     5  *
     6  *     // @return true if this NestedInteger holds a single integer, rather than a nested list.
     7  *     public boolean isInteger();
     8  *
     9  *     // @return the single integer that this NestedInteger holds, if it holds a single integer
    10  *     // Return null if this NestedInteger holds a nested list
    11  *     public Integer getInteger();
    12  *
    13  *     // @return the nested list that this NestedInteger holds, if it holds a nested list
    14  *     // Return null if this NestedInteger holds a single integer
    15  *     public List<NestedInteger> getList();
    16  * }
    17  */
    18 public class NestedIterator implements Iterator<Integer> {
    19     
    20     List<NestedInteger> list;
    21     NestedIterator curIterator;
    22     int nextIndex;
    23     boolean isNestedList;
    24     boolean hasNext;
    25 
    26     public NestedIterator(List<NestedInteger> nestedList) {
    27         list = nestedList;
    28         nextIndex = -1;
    29         moveToNext();
    30     }
    31 
    32     @Override
    33     public Integer next() {
    34         Integer nextVal= null;
    35         if (isNestedList){
    36             nextVal = curIterator.next();
    37             if (!curIterator.hasNext()) moveToNext();
    38         } else {
    39             nextVal = list.get(nextIndex).getInteger();
    40             moveToNext();
    41         }
    42         return nextVal;
    43     }
    44 
    45     @Override
    46     public boolean hasNext() {
    47         return hasNext;
    48     }
    49     
    50     public void moveToNext(){
    51         while (true){
    52             nextIndex++;
    53             if (nextIndex>=list.size()){
    54                 hasNext = false;
    55                 return;
    56             }
    57         
    58             isNestedList = !list.get(nextIndex).isInteger();
    59             if (isNestedList){
    60                 curIterator = new NestedIterator(list.get(nextIndex).getList());
    61                 if (!curIterator.hasNext()) continue;
    62             } else curIterator = null;
    63             hasNext = true;
    64             return;
    65         }
    66     }
    67 }
    68 
    69 /**
    70  * Your NestedIterator object will be instantiated and called as such:
    71  * NestedIterator i = new NestedIterator(nestedList);
    72  * while (i.hasNext()) v[f()] = i.next();
    73  */
  • 相关阅读:
    jQuery给动态添加的元素绑定事件的方法
    无法加载父级样式或设置IIS的asp站点启用父路径
    ASP.NET MVC 必须设置 ErrorMessageString 或 ErrorMessageResourceName,但不能同时设置二者。
    layer弹窗监控键盘事件
    js 刷新windows.open另一个窗口页面或window.open的页面如何刷新(父页面)上层页面
    jquery提交表单,回调函数
    iis Server Error in '/' Application
    jni&&jvmti&&JMC
    Backpropagation
    k-means-algorithm
  • 原文地址:https://www.cnblogs.com/lishiblog/p/5740853.html
Copyright © 2011-2022 走看看