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. css样式中遇到!important

    链接: http://zhidao.baidu.com/link?url=XyYHS2l-bFkzEgMBWfbQYuEV4vmGz8kOhj-jDL_HBqu0KnVIBQJKvw5OOO-bVjv ...

  2. 用C语言将搜狗输入法词库转换成QQ拼音输入法词库

    搜狗输入法词库格式: 'ni'kan'xia 你看下 'ni'kan'xia'gai'hou 你看下改后 'ni'kan'xing'ma 你看行吗 'ni'kan'zen'me'yang 你看怎么样 ...

  3. [Bug]IIs Cannot read configuration file due to insufficient permissions

    摘要 在部署站点的时候,遇到这样的问题Cannot read configuration file due to insufficient permissions 解决办法 在服务器上部署站点,浏览的 ...

  4. mysql explain详解

    对于经常使用mysql的兄弟们,对explain一定不会陌生.当你在一条SELECT语句前放上关键词EXPLAIN,MySQL解释它将如何处理SELECT,提供有关表如何联合和以什么次序的信息.借助于 ...

  5. ktouch移动端事件库

    最近闲来无事,写了个移动端的事件库,代码贴在下面,大家勿拍. /** @version 1.0.0 @author gangli @deprecated 移动端触摸事件库 */ (function ( ...

  6. Tomcat 6.0 简介

    本片翻译来自:http://tomcat.apache.org/tomcat-6.0-doc/introduction.html 介绍 无论是开发者还是tomcat管理员在使用前都需要了解一些必要的信 ...

  7. Tomcat 部署详解

    前篇大概讲过了tomcat的发布和部署,以及测试. 本篇详细介绍下tomcat的部署相关的内容,本篇主要内容: 1 介绍下部署相关的内容,需要安装的文件以及上下文描述符 2 静态部署——在tomcat ...

  8. findByExample(Object exampleEntity)方法得到的List判断是否为空,不可用(lis != null)

    用findByExample(Object exampleEntity)方法可以应用在用户登录上面,获得有登陆名和密码的user对象进行查询. 返回两者都符合的对象列表,为空则登陆失败. 错误的方法: ...

  9. mybatis 添加事物后 无法获取自增主键的问题

    检查代码后没发现mapper文件设置自增主键返回的问题,后来检查到,关闭事务后,执行完是可以获取返回的主键的, 我在mysql的客户端里关闭自动提交,发现使用select last_insert_id ...

  10. git学习教程

    猴子都能懂的GIT入门 http://backlogtool.com/git-guide/cn/ 廖雪峰的Git教程 http://www.liaoxuefeng.com/wiki/001373951 ...