LeetCode-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].

Follow up:
As an added challenge, try to code it using only iterators in C++ or iterators in Java.

Solution:

 1 public class Vector2D implements Iterator<Integer> {
 2     List<List<Integer>> vecs;
 3     int curIndex;
 4     Iterator<Integer> iter;
 5
 6     public Vector2D(List<List<Integer>> vec2d) {
 7         vecs = vec2d;
 8         curIndex = 0;
 9         if (curIndex >= vecs.size()) return;
10         iter = vecs.get(curIndex).iterator();
11         moveToNext();
12     }
13
14     private void moveToNext() {
15         while (!iter.hasNext()) {
16             curIndex++;
17             if (curIndex >= vecs.size())
18                 break;
19             iter = vecs.get(curIndex).iterator();
20         }
21     }
22
23     @Override
24     public Integer next() {
25         if (curIndex >= vecs.size())
26             return null;
27
28         Integer res = iter.next();
29         moveToNext();
30
31         return res;
32     }
33
34     @Override
35     public boolean hasNext() {
36         return curIndex < vecs.size();
37     }
38 }
时间: 2024-11-05 11:41:57

LeetCode-Flatten 2D Vector的相关文章

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]. Hint: How many variables do y

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]. Hint: How many variables do y

251. Flatten 2D Vector 平铺二维矩阵

[抄题]: Implement an iterator to flatten a 2d vector. Example: Input: 2d vector = [ [1,2], [3], [4,5,6] ] Output: [1,2,3,4,5,6] Explanation: By calling next repeatedly until hasNext returns false,   the order of elements returned by next should be: [1,

[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 hasN

[leetcode]Flatten Binary Tree to Linked List @ Python

原题地址:http://oj.leetcode.com/problems/flatten-binary-tree-to-linked-list/ 题意: Given a binary tree, flatten it to a linked list in-place. For example,Given 1 / 2 5 / \ 3 4 6 The flattened tree should look like: 1 2 3 4 5 6Hints: If you notice carefully

LeetCode: Flatten Binary Tree to Linked List [114]

[题目] Given a binary tree, flatten it to a linked list in-place. For example, Given 1 / 2 5 / \ 3 4 6 The flattened tree should look like: 1 2 3 4 5 6 click to show hints. [题意] 将一颗二叉树转化为链表,right充当next指针,元素顺序为先序遍历的循序.不使用额外的空间 [思路] 先对左子树链表化,然后将右子树链表化.然后

LeetCode——Flatten Binary Tree to Linked List

Given a binary tree, flatten it to a linked list in-place. For example, Given 1 / 2 5 / \ 3 4 6 The flattened tree should look like: 1 2 3 4 5 6 Hints: If you notice carefully in the flattened tree, each node's right child points to the next node of

LeetCode &quot;Flatten Nested List Iterator&quot;

Besides the common stack-based solutions, here is another recursive NestedIterator solution :) class NestedIterator { int currInx; const vector<NestedInteger> &rData; NestedIterator *sub; void move() { if (currInx < 0 || rData[currInx].isInte

[LeetCode] Flatten Binary Tree to Linked List 将二叉树展开成链表

Given a binary tree, flatten it to a linked list in-place. For example,Given 1 / 2 5 / \ 3 4 6 The flattened tree should look like: 1 2 3 4 5 6 click to show hints. Hints: If you notice carefully in the flattened tree, each node's right child points