Chapter 33 Iterator

Iterator顾名思义用iterative的方法解.递归的数据结构用迭代的方法解?这时候经常就需要stack的帮助.

C++ stack: http://en.cppreference.com/w/cpp/container/stack

33.1 173. Binary Search Tree Iterator

Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the root node of a BST.

Calling next() will return the next smallest number in the BST.

Note: next() and hasNext() should run in average \(O(1)\) time and uses \(O(h)\) memory, where h is the height of the tree.

https://leetcode.com/problems/binary-search-tree-iterator/

如果不是题目对space complexity的要求,这题解法根据S分为:

S: O(1) - morris traversal
S: O(H) - stack
S: O(N) - do work at constructor

S:O(H)的解法如下:
维护一个栈,从根节点开始,每次迭代地将根节点的左孩子压入栈,直到左孩子为空为止. 调用next()方法时,弹出栈顶,如果被弹出的元素拥有右孩子,则以右孩子为根,将其左孩子迭代压栈.

S:O(N)的解法是: 采用根的中序遍历法将树节点保存在list中也可以实现树的迭代.但是空间复杂度\(O(N)\),N为树的大小,不满足题目要求

S:O(1)的解法(Morris)如下:

33.2 284. Peeking Iterator

https://leetcode.com/problems/peeking-iterator/

If we know the underlying data structure, this question will be very easy. The trikcy part here is you can not access underlying data structure directly, instead you have to use the interface provided by the base class

C++:

这里有个C++的语法问题,如何从child class里面调用parent class. 注意这里没有用override,因为没有virtual function. Overload和override的区别?

JAVA:

33.3 341. Flatten Nested List Iterator

https://leetcode.com/problems/flatten-nested-list-iterator/

这道题让我们建立压平嵌套链表的迭代器,关于嵌套链表的数据结构最早出现在Nested List Weight Sum中,而那道题是用的递归的方法来解的,而迭代器一般都是用迭代的方法来解的,而递归一般都需用栈来辅助遍历,由于栈的后进先出的特性,我们在对向量遍历的时候,从后往前把对象压入栈中,那么第一个对象最后压入栈就会第一个取出来处理,我们的hasNext()函数需要遍历栈,并进行处理,如果栈顶元素是整数,直接返回true,如果不是,那么移除栈顶元素,并开始遍历这个取出的list,还是从后往前压入栈,循环停止条件是栈为空,返回false.

可不可以把push stack的任务放在next()函数中做呢?答案是不可以,因为有edge case: [[]], [[[[]]],[]],如果不对其解封,根本不知道是否有next.

注意就是stack是LIFO的,所以push的时候是从后往前loop.

如果要强行用递归,在构造函数的时候就利用递归的方法把这个嵌套链表全部压平展开.其实程序一般初始化的时间长点无所谓,只要运行起来快就行.所以可以这样:

如果数据是静态的,其实第二个方法要好.如果这个数据是动态的,可以把第一个方法的stack改成deque或者list,可以在hasNext被调用之前往里面加入新的数据,虽然这不是典型迭代器的用法.

第二个方法并没有使用getList().size(),如果没有size().第二种方法显得更加方便.

另一种写法:

33.4 281. Zigzag Iterator

Given two 1d vectors, implement an iterator to return their elements alternately.

For example, given two 1d vectors:

v1 = [1, 2]
v2 = [3, 4, 5, 6]

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

Follow up: What if you are given k 1d vectors? How well can your code be extended to such cases?

Clarification for the follow up question - Update (2015-09-18): The “Zigzag” order is not clearly defined and is ambiguous for k > 2 cases. If “Zigzag” does not look right to you, replace “Zigzag” with “Cyclic”. For example, given the following input:

[1,2,3]
[4,5,6,7]
[8,9]

It should return [1,4,8,2,5,9,3,6,7].

https://leetcode.com/problems/zigzag-iterator/

33.5 251. Flatten 2D Vector

Implement an iterator to flatten a 2d vector.

For example, Given 2d vector =

[
  [1,2],
  [3],
  [4,5,6]
]

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

https://leetcode.com/problems/flatten-2d-vector/

  • CPP
  • Java
  • Python

33.6 604. Design Compressed String Iterator

Design and implement a data structure for a compressed string iterator. It should support the following operations: next and hasNext.

The given compressed string will be in the form of each letter followed by a positive integer representing the number of this letter existing in the original uncompressed string.

next() - if the original string still has uncompressed characters, return the next letter; Otherwise return a white space.
hasNext() - Judge whether there is any letter needs to be uncompressed.

Note: Please remember to RESET your class variables declared in StringIterator, as static/class variables are persisted across multiple test cases. Please see here for more details.

Example:

StringIterator iterator = new StringIterator("L1e2t1C1o1d1e1");
iterator.next(); // return 'L'
iterator.next(); // return 'e'
iterator.next(); // return 'e'
iterator.next(); // return 't'
iterator.next(); // return 'C'
iterator.next(); // return 'o'
iterator.next(); // return 'd'
iterator.hasNext(); // return true
iterator.next(); // return 'e'
iterator.hasNext(); // return false
iterator.next(); // return ' '

https://leetcode.com/problems/design-compressed-string-iterator

http://bit.ly/2wOycnY