LeetCode-Flatten Nested List Iterator

來源:互聯網
上載者:User

標籤:

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 integer10  *     // Return null if this NestedInteger holds a nested list11  *     public Integer getInteger();12  *13  *     // @return the nested list that this NestedInteger holds, if it holds a nested list14  *     // Return null if this NestedInteger holds a single integer15  *     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     @Override33     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     @Override46     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  */

 

LeetCode-Flatten Nested List Iterator

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.