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://segmentfault.com/a/1190000003791233

用一个数组表示每个List的迭代器,然后再记录一个变量,用来表示当前用到了第几个迭代器。

 public class Vector2D {

     List<Iterator<Integer>> its;
int curr = ; public Vector2D(List<List<Integer>> vec2d) {
this.its = new ArrayList<Iterator<Integer>>();
for (List<Integer> l : vec2d) {
// 只将非空的迭代器加入数组
if (l.size() > ) {
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();
}
}

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 Flatten 2D Vector

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

  3. 251. Flatten 2D Vector

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

  4. [Locked] Flatten 2D Vector

    Problem Description: Implement an iterator to flatten a 2d vector. For example,Given 2d vector = [ [ ...

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

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

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

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

  7. LeetCode 251. Flatten 2D Vector

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

  8. Flatten 2D Vector -- LeetCode

    Implement an iterator to flatten a 2d vector. For example,Given 2d vector = [ [,], [], [,,] ] By cal ...

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

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

随机推荐

  1. 由Collections.unmodifiableList引发的重构

    原文  http://www.cnblogs.com/persist-confident/p/4516741.html 今天阅读源码的时候,无意中看到了Collections.unmodifiable ...

  2. asp.net mvc 4 高级编程学习笔记:第四章 模型

    数据模型 数据模型及O/R转化,采用EntityFramework实现. 可以采用firstCode模型,首先定义模型,通过模型生成数据库,也可以通过安装EFPowerTools,通过数据库自动生成对 ...

  3. [C#]System.Timers.Timer

    摘要 在.Net中有几种定时器,最喜欢用的是System.Timers命名空间下的定时器,使用起来比较简单,作为定时任务,有Quartz.net,但有时候,一个非常简单的任务,不想引入这个定时任务框架 ...

  4. C# DateTime和String转换

    "; DateTime.ParseExact(time,"yyyyMMdd",System.Globalization.DateTimeFormatInfo.Curren ...

  5. VTK初学一,e_Triangle_CellArray三角形的另一种方法绘制

    #ifndef INITIAL_OPENGL #define INITIAL_OPENGL #include <vtkAutoInit.h> VTK_MODULE_INIT(vtkRend ...

  6. Apache服务器httpd.exe进程占用cpu超过50%的解决方法

    httpd.exe进程占用cpu超过50%,关闭掉Apache服务,cpu应用率立刻下降到0.  重新启动Apache又出现占用cpu高的情况.  原因是:httpd.exe和防火墙配置有冲突. 解决 ...

  7. bootstrap搜索框样式代码及效果

    <div class="container"> <div class="input-group"> <input type=&qu ...

  8. 微信小程序未来怎么样?听微盟卫晓祥来说说

    微信小程序宣布公测已经一个多月了,开发者一片火热,未来会怎么样?听微盟卫晓祥来说说.微盟移动营销事业部总经理卫晓祥表示,微信小程序最吸引商户的地方在于:一方面小程序作为一种全新的连接用户与服务的方式, ...

  9. hdu.1429.胜利大逃亡(续)(bfs + 0101011110)

    胜利大逃亡(续) Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total S ...

  10. hash-6.CopyOnWriteArrayList

    1.ArrayList的add方法 public boolean add(E e) { ensureCapacityInternal(size + 1); // Increments modCount ...