原题链接在这里:https://leetcode.com/problems/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,2,3,4,5,6].

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

题解:

用两个index 分别记录list 的 index 和当前 list的element index.

Time Complexity: Vector2D() O(1). hasNext() O(vec2d.size()). next() O(1). Space: O(1).

AC Java:

 public class Vector2D {
List<List<Integer>> listOfList;
int listIndex;
int elemIndex;
public Vector2D(List<List<Integer>> vec2d) {
listOfList = vec2d;
listIndex = 0;
elemIndex = 0;
} public int next() {
return listOfList.get(listIndex).get(elemIndex++);
} public boolean hasNext() {
while(listIndex < listOfList.size()){
if(elemIndex < listOfList.get(listIndex).size()){
return true;
}else{
listIndex++;
elemIndex = 0;
}
}
return false;
}
} /**
* Your Vector2D object will be instantiated and called as such:
* Vector2D i = new Vector2D(vec2d);
* while (i.hasNext()) v[f()] = i.next();
*/

Follow up 要用Iterator class.

Time Complexity:  Vector2D() O(1). hasNext() O(vec2d.size()). next() O(1). Space: O(1).

AC Java:

 public class Vector2D implements Iterator<Integer> {
Iterator<List<Integer>> i;
Iterator<Integer> j; public Vector2D(List<List<Integer>> vec2d) {
i = vec2d.iterator();
} @Override
public Integer next() {
return j.next();
} @Override
public boolean hasNext() {
while((j==null || !j.hasNext()) && i.hasNext()){
j = i.next().iterator();
} return j!=null && j.hasNext();
}
} /**
* Your Vector2D object will be instantiated and called as such:
* Vector2D i = new Vector2D(vec2d);
* while (i.hasNext()) v[f()] = i.next();
*/

类似Flatten Nested List Iterator.

LeetCode 251. Flatten 2D Vector的更多相关文章

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

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

  2. 251. Flatten 2D Vector

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

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

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

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

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

  5. LeetCode Flatten 2D Vector

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

  6. Flatten 2D Vector -- LeetCode

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

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

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

  9. [Locked] Flatten 2D Vector

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

随机推荐

  1. P4121 [WC2005]双面棋盘

    题目 P4121 [WC2005]双面棋盘 貌似是刘汝佳出的题目?? 做法 线段树维护并查集 线段树分治\(1\)~\(n\)行,我们要考虑维护的肯定是黑.白各自的联通块数量 考虑区间合并,其实就与中 ...

  2. Linux 邮件服务搭建

    Linux 邮件服务搭建 邮件服务针对,在大型企业使用的比较多,一般小型企业都会买一些邮件服务,或者使用一些免费的邮件服务,达到我们使用的需求,并且不需要自己维护,下面我就来简单安装一下两个邮箱的案例 ...

  3. uboot无法引导uImage错误及其解决方法

    先编译友善提供的linux内核: make ARCH=arm mini2440_defconfigmake CROSS_COMPILE=arm-linux- uImage 在arch/arm/boot ...

  4. win7 与 Ubuntu 16.04 文件传送

    win7 与 Ubuntu 16.04 文件传送 环境:主机系统为win7,虚拟机为vmware12, 虚拟系统为ubuntu 16.04 方案一: 通过虚拟机vmware的共享文件夹实现. 方案二: ...

  5. iOS_XML与JSON解析

    XML与JSON简介 XML 可扩展标记语言 用于标记电子文件使其具有结构性的标记语言,可以用来标记数据.定义数据类型,是一种允许用户对自己的标记语言进行定义的源语言 易读性高,编码手写难度小,数据量 ...

  6. 在Linux系统下使用Docker以及Weave搭建Nginx反向代理

    Hi, 今天我们将会学习如何使用 Weave 和 Docker 搭建 Nginx 的反向代理/负载均衡服务器.Weave 可以创建一个虚拟网络将 Docker 容器彼此连接在一起,支持跨主机部署及自动 ...

  7. nginx的理解

    1.静态HTTP服务器 首先,Nginx是一个HTTP服务器,可以将服务器上的静态文件(如HTML.图片)通过HTTP协议展现给客户端. 配置: 2.反向代理服务器 什么是反向代理? 客户端本来可以直 ...

  8. maven 一个简单项目 —— maven权威指南学习笔记(三)

    目标: 对构建生命周期 (build  lifecycle),Maven仓库 (repositories),依赖管理 (dependency management)和项目对象模型 (Project O ...

  9. 使用shell自动备份数据库

    全备份 #!/bin/sh #mysql地址 #检测用户是否手动输入了密码 mysql_host="" #mysql用户 mysql_user="" #mysq ...

  10. mysql一次运行多个SQL文件

    在文件 batch.sql 中写下多个SQL文件 source file1.SQLsource file2.SQLsource file3.SQL 然后运行 source batch.sql