[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]
.
Hint:
- How many variables do you need to keep track?
- Two variables is all you need. Try with
x
andy
. - Beware of empty rows. It could be the first few rows.
- To write correct code, think about the invariant to maintain. What is it?
- The invariant is
x
andy
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? - Not sure? Think about how you would implement
hasNext()
. Which is more complex? - 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 函数即为取出当前位置元素,坐标后移一位,参见代码如下:
解法一:
- class Vector2D {
- public:
- Vector2D(vector<vector<int>>& vec2d) {
- for (auto a : vec2d) {
- v.insert(v.end(), a.begin(), a.end());
- }
- }
- int next() {
- return v[i++];
- }
- bool hasNext() {
- return i < v.size();
- }
- private:
- vector<int> v;
- int i = ;
- };
下面我们来看另一种解法,不直接转换为一维数组,而是维护两个变量x和y,将x和y初始化为0,对于 hasNext 函数,检查当前x是否小于总行数,y是否和当前行的列数相同,如果相同,说明要转到下一行,则x自增1,y初始化为0,若此时x还是小于总行数,说明下一个值可以被取出来,那么在 next 函数就可以直接取出行为x,列为y的数字,并将y自增1,参见代码如下:
解法二:
- class Vector2D {
- public:
- Vector2D(vector<vector<int>>& vec2d): data(vec2d), x(), y() {}
- int next() {
- hasNext();
- return data[x][y++];
- }
- bool hasNext() {
- while (x < data.size() && y == data[x].size()) {
- ++x;
- y = ;
- }
- return x < data.size();
- }
- private:
- vector<vector<int>> data;
- int x, y;
- };
题目中的 Follow up 让我们用 interator 来做,C++中 iterator 不像 Java 中的那么强大,自己本身并没有包含 next 和 hasNext 函数,所以得自己来实现,将x定义为行的 iterator,再用个 end 指向二维数组的末尾,定义一个整型变量y来指向列位置,实现思路和上一种解法完全相同,只是写法略有不同,参见代码如下:
解法三:
- class Vector2D {
- public:
- Vector2D(vector<vector<int>>& vec2d): x(vec2d.begin()), end(vec2d.end()) {}
- int next() {
- hasNext();
- return (*x)[y++];
- }
- bool hasNext() {
- while (x != end && y == (*x).size()) {
- ++x;
- y = ;
- }
- return x != end;
- }
- private:
- vector<vector<int>>::iterator x, end;
- int y = ;
- };
Github 同步地址:
https://github.com/grandyang/leetcode/issues/251
类似题目:
参考资料:
https://leetcode.com/problems/flatten-2d-vector/
LeetCode All in One 题目讲解汇总(持续更新中...)
[LeetCode] Flatten 2D Vector 压平二维向量的更多相关文章
- [LeetCode] 251. Flatten 2D Vector 压平二维向量
Implement an iterator to flatten a 2d vector. For example,Given 2d vector = [ [1,2], [3], [4,5,6] ] ...
- 用vector实现二维向量
如果一个向量的每一个元素是一个向量,则称为二维向量,例如 vector<vector<int> >vv(3, vector<int>(4));//这里,两个“> ...
- LeetCode Flatten 2D Vector
原题链接在这里:https://leetcode.com/problems/flatten-2d-vector/ 题目: Implement an iterator to flatten a 2d v ...
- [Swift]LeetCode251.展平二维向量 $ Flatten 2D Vector
Implement an iterator to flatten a 2d vector. For example,Given 2d vector = [ [1,2], [3], [4,5,6] ] ...
- [VB.NET][C#]二维向量的基本运算
前言 在数学中,几何向量指具有大小(Magnitude)和方向的几何对象,它在线性代数中经由抽象化有着更一般的概念.向量在编程中也有着及其广泛的应用,其作用在图形编程和游戏物理引擎方面尤为突出. 基于 ...
- uda 3.C++二维向量
二维向量 接下来,你将使用向量来存储矩阵.就像 Python 使用列表列表来存储矩阵一样,C++ 使用的是向量的向量.用于声明二维向量的语法有点复杂. 假设你正在使用 Python,并且想存储一个 3 ...
- 【Unity3D】计算二维向量夹角(-180到180)
在Unity3D中,有时候我们需要计算二维向量的夹角.二维向量夹角一般在0~180度之前,可以直接调用Vector2.Angle(Vector2 from, Vector2 to)来计算. 但是在有些 ...
- 251. Flatten 2D Vector 平铺二维矩阵
[抄题]: Implement an iterator to flatten a 2d vector. Example: Input: 2d vector = [ [1,2], [3], [4,5,6 ...
- LeetCode 251. Flatten 2D Vector
原题链接在这里:https://leetcode.com/problems/flatten-2d-vector/ 题目: Implement an iterator to flatten a 2d v ...
随机推荐
- C#组件系列———又一款日志组件:Elmah的学习和分享
前言:好久没动笔了,都有点生疏,12月都要接近尾声,可是这月连一篇的产出都没有,不能坏了“规矩”,今天还是来写一篇.最近个把月确实很忙,不过每天早上还是会抽空来园子里逛逛.一如既往,园子里每年这个时候 ...
- 通过pycharm使用git[图文详解]
前言 使用git+pycharm有一段时间了,算是稍有点心得,这边整理一下,可能有的方法不是最优,欢迎交流,可能还是习惯敲命令去使用git,不过其实pycharm已经帮忙做了很多了,我们可以不用记住那 ...
- 如果你也会C#,那不妨了解下F#(5):模块、与C#互相调用
F# 项目 在之前的几篇文章介绍的代码都在交互窗口(fsi.exe)里运行,但平常开发的软件程序可能含有大类类型和函数定义,代码不可能都在一个文件里.下面我们来看VS里提供的F#项目模板. F#项目模 ...
- Basic Tutorials of Redis(9) -First Edition RedisHelper
After learning the basic opreation of Redis,we should take some time to summarize the usage. And I w ...
- Sqlserver调用api
虽然使用sqlserver去调用服务接口的情况比较少,但也可以去了解下对应的使用情况 一.首先要开启组件的配置 sp_configure ; GO RECONFIGURE; GO sp_configu ...
- LINQ to SQL语句(14)之Null语义和DateTime
Null语义 说明:下面第一个例子说明查询ReportsToEmployee为null的雇员.第二个例子使用Nullable<T>.HasValue查询雇员,其结果与第一个例子相同.在第三 ...
- 【C#进阶系列】27 I/O限制的异步操作
上一章讲到了用线程池,任务,并行类的函数,PLINQ等各种方式进行基于线程池的计算限制异步操作. 而本章讲的是如何异步执行I/O限制操作,允许将任务交给硬件设备来处理,期间完全不占用线程和CPU资源. ...
- bzoj1001--最大流转最短路
http://www.lydsy.com/JudgeOnline/problem.php?id=1001 思路:这应该算是经典的最大流求最小割吧.不过题目中n,m<=1000,用最大流会TLE, ...
- Yii 2.x 多主题 - 多语言 配置
语言:只要在原来模板的位置建立语言目录 多主题:要重新定义模板的根目录
- spring mvc 和spring security配置 spring-servlet.xml和spring-security.xml设置
spring-servlet.xml配置 <?xml version="1.0" encoding="UTF-8"?> <beans xmln ...