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:

  1. How many variables do you need to keep track?
  2. Two variables is all you need. Try with x and y.
  3. Beware of empty rows. It could be the first few rows.
  4. To write correct code, think about the invariant to maintain. What is it?
  5. The invariant is x and y must always point to a valid point in the 2d vector. Should you maintain your invariant ahead of time or right when you need it?
  6. Not sure? Think about how you would implement hasNext(). Which is more complex?
  7. Common logic in two different places should be refactored into a common method.

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

给一个二维向量数组压平输出为一个数组。要实现一个iterator,包括next和hasNext函数。

解法:将二维数组按顺序先存入到一个一维数组里,然后维护一个变量i来记录当前遍历到的位置,hasNext函数看当前坐标是否小于元素总数,next函数即为取出当前位置元素。

Java:one iterator

public class Vector2D {

    List<Iterator<Integer>> its;
int curr = 0; public Vector2D(List<List<Integer>> vec2d) {
this.its = new ArrayList<Iterator<Integer>>();
for(List<Integer> l : vec2d){
// 只将非空的迭代器加入数组
if(l.size() > 0){
this.its.add(l.iterator());
}
}
} public int next() {
Integer res = its.get(curr).next();
// 如果该迭代器用完了,换到下一个
if(!its.get(curr).hasNext()){
curr++;
}
return res;
} public boolean hasNext() {
return curr < its.size() && its.get(curr).hasNext();
}
}

Java: two iterator

public class Vector2D {

    Iterator<List<Integer>> it;
Iterator<Integer> curr; public Vector2D(List<List<Integer>> vec2d) {
it = vec2d.iterator();
} public int next() {
hasNext();
return curr.next();
} public boolean hasNext() {
// 当前列表的迭代器为空,或者当前迭代器中没有下一个值时,需要更新为下一个迭代器
while((curr == null || !curr.hasNext()) && it.hasNext()){
curr = it.next().iterator();
}
return curr != null && curr.hasNext();
}
}

Java:

public class Vector2D {

    private List<List<Integer>> vec2d;
private int rowId;
private int colId;
private int numRows; public Vector2D(List<List<Integer>> vec2d) {
this.vec2d = vec2d;
rowId = 0;
colId = 0;
numRows = vec2d.size();
} public int next() {
int ans = 0; if (colId < vec2d.get(rowId).size()) {
ans = vec2d.get(rowId).get(colId);
} colId++; if (colId == vec2d.get(rowId).size()) {
colId = 0;
rowId++;
} return ans;
} public boolean hasNext() {
while (rowId < numRows && (vec2d.get(rowId) == null || vec2d.get(rowId).isEmpty())) {
rowId++;
} return vec2d != null &&
!vec2d.isEmpty() &&
rowId < numRows;
}
}  

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

public class Vector2D {
private Iterator<List<Integer>>outerIterator;
private Iterator<Integer> innerIterator; public Vector2D(List<List<Integer>> vec2d) {
outerIterator = vec2d.iterator();
innerIterator = Collections.emptyIterator();
} public int next() {
return innerIterator.next();
} public boolean hasNext() {
if (innerIterator.hasNext()) {
return true;
} if (!outerIterator.hasNext()) {
return false;
} innerIterator = outerIterator.next().iterator(); return hasNext();
}
} /**
* Your Vector2D object will be instantiated and called as such:
* Vector2D i = new Vector2D(vec2d);
* while (i.hasNext()) v[f()] = i.next();
*/  

Python:

class Vector2D(object):

    # @param vec2d {List[List[int]]}
def __init__(self, vec2d):
# Initialize your data structure here
self.row, self.col, self.vec2d = 0, 0, vec2d # @return {int} a next element
def next(self):
# Write your code here
self.col += 1
return self.vec2d[self.row][self.col - 1] # @return {boolean} true if it has next element
# or false
def hasNext(self):
# Write your code here
while self.row < len(self.vec2d) and \
self.col >= len(self.vec2d[self.row]):
self.row, self.col = self.row + 1, 0
return self.row < len(self.vec2d) # Your Vector2D object will be instantiated and called as such:
# i, v = Vector2D(vec2d), []
# while i.hasNext(): v.append(i.next())

C++:

class Vector2D {
public:
Vector2D(vector<vector<int>>& vec2d) {
// Initialize your data structure here
begin = vec2d.begin();
end = vec2d.end();
pos = 0;
} int next() {
// Write your code here
hasNext();
return (*begin)[pos++];
} bool hasNext() {
// Write your code here
while (begin != end && pos == (*begin).size())
begin++, pos = 0;
return begin != end;
} private:
vector<vector<int>>::iterator begin, end;
int pos;
}; /**
* Your Vector2D object will be instantiated and called as such:
* Vector2D i(vec2d);
* while (i.hasNext()) cout << i.next();
*/

  

类似题目:

[LeetCode] 341. Flatten Nested List Iterator 压平嵌套链表迭代器

All LeetCode Questions List 题目汇总

[LeetCode] 251. Flatten 2D Vector 压平二维向量的更多相关文章

  1. [LeetCode] Flatten 2D Vector 压平二维向量

    Implement an iterator to flatten a 2d vector. For example,Given 2d vector = [ [1,2], [3], [4,5,6] ] ...

  2. LeetCode 251. Flatten 2D Vector

    原题链接在这里:https://leetcode.com/problems/flatten-2d-vector/ 题目: Implement an iterator to flatten a 2d v ...

  3. 用vector实现二维向量

    如果一个向量的每一个元素是一个向量,则称为二维向量,例如 vector<vector<int> >vv(3, vector<int>(4));//这里,两个“> ...

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

    [抄题]: Implement an iterator to flatten a 2d vector. Example: Input: 2d vector = [ [1,2], [3], [4,5,6 ...

  5. 251. Flatten 2D Vector

    题目: Implement an iterator to flatten a 2d vector. For example,Given 2d vector = [ [1,2], [3], [4,5,6 ...

  6. [Leetcode] search a 2d matrix 搜索二维矩阵

    Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the follo ...

  7. [Swift]LeetCode251.展平二维向量 $ Flatten 2D Vector

    Implement an iterator to flatten a 2d vector. For example,Given 2d vector = [ [1,2], [3], [4,5,6] ] ...

  8. Leetcode之二分法专题-240. 搜索二维矩阵 II(Search a 2D Matrix II)

    Leetcode之二分法专题-240. 搜索二维矩阵 II(Search a 2D Matrix II) 编写一个高效的算法来搜索 m x n 矩阵 matrix 中的一个目标值 target.该矩阵 ...

  9. [VB.NET][C#]二维向量的基本运算

    前言 在数学中,几何向量指具有大小(Magnitude)和方向的几何对象,它在线性代数中经由抽象化有着更一般的概念.向量在编程中也有着及其广泛的应用,其作用在图形编程和游戏物理引擎方面尤为突出. 基于 ...

随机推荐

  1. C.Minimum Array(二分+set)

    题目描述: 知识点: lower_bound和uper_bound lower_bound(起始地址,结束地址,要查找的数值) 返回的是数值 第一个 出现的位置. upper_bound(起始地址,结 ...

  2. Proxmox初步了解

    Proxmox不分主从,所有节点同步信息 创建集群 pvecm(可通过web界面创建.添加至集群) pvecm create cluster01 pvecm status 添加节点 pvecm add ...

  3. zookeeper 的 docker 镜像使用

    dockerhub 网址:https://hub.docker.com/_/zookeeper

  4. SQL中的trim函数

    Oracle TRIM函数是很常见的函数,下面对Oracle TRIM函数的语法作了详尽的阐述说明,希望可以让您对Oracle TRIM函数有更深的认识. 如果提到Oracle TRIM函数,最简单的 ...

  5. RMQ问题及ST表

    RMQ(Range Minimum/Maximum Query)问题指的是一类对于给定序列,要求支持查询某区间内的最大.最小值的问题.很显然,如果暴力预处理的话复杂度为 \(O(n^2)\),而此类问 ...

  6. vue cli4.0 快速搭建项目详解

    搭建项目之前,请确认好你自己已经安装过node, npm, vue cli.没安装的可以参考下面的链接安装. 如何安装node? 安装好node默认已经安装好npm了,所以不用单独安装了. 如何安装v ...

  7. 04-Flutter移动电商实战-打通底部导航栏

    关于界面切换以及底栏的实现可参考之前写的一篇文章:Flutter实 ViewPager.bottomNavigationBar界面切换 1.新建4个基本dart文件 在pages目录下,我们新建下面四 ...

  8. Mscordacwks.dll/SOS.dll 调试归档

    找到个好东西 为什么要归档 此存档提供帮助,并可能提供对以下问题的答案 是否可以使WinDBG在符号存储中找到mscordacwks.dll?, Windbg需要不同版本的mscordacwks.dl ...

  9. Python 下载超大文件

    使用python下载超大文件, 直接全部下载, 文件过大, 可能会造成内存不足, 这时候要使用requests 的 stream模式, 主要代码如下 iter_content:一块一块的遍历要下载的内 ...

  10. 洛谷 P1948 [USACO08JAN]电话线Telephone Lines 题解

    P1948 [USACO08JAN]电话线Telephone Lines 题目描述 Farmer John wants to set up a telephone line at his farm. ...