251. Flatten 2D Vector
双指针+vector
Design an iterator to flatten a 2D vector. It should support the next
and hasNext
operations.
Implement the Vector2D
class:
Vector2D(int[][] vec)
initializes the object with the 2D vectorvec
.next()
returns the next element from the 2D vector and moves the pointer one step forward. You may assume that all the calls tonext
are valid.hasNext()
returnstrue
if there are still some elements in the vector, andfalse
otherwise.
Example 1:
Constraints:
0 <= vec.length <= 200
0 <= vec[i].length <= 500
-500 <= vec[i][j] <= 500
At most
10
5
calls will be made tonext
andhasNext
.
分析
lazy evaluation.
advance函数负责外指针移动,next 负责内指针,hasnext判断外指针
Last updated
Was this helpful?