LeetCode 251. Flatten 2D Vector
原题链接在这里: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的更多相关文章
- [LeetCode] 251. Flatten 2D Vector 压平二维向量
Implement an iterator to flatten a 2d vector. For example,Given 2d vector = [ [1,2], [3], [4,5,6] ] ...
- 251. Flatten 2D Vector
题目: Implement an iterator to flatten a 2d vector. For example,Given 2d vector = [ [1,2], [3], [4,5,6 ...
- 251. Flatten 2D Vector 平铺二维矩阵
[抄题]: Implement an iterator to flatten a 2d vector. Example: Input: 2d vector = [ [1,2], [3], [4,5,6 ...
- [LeetCode] Flatten 2D Vector 压平二维向量
Implement an iterator to flatten a 2d vector. For example,Given 2d vector = [ [1,2], [3], [4,5,6] ] ...
- LeetCode Flatten 2D Vector
原题链接在这里:https://leetcode.com/problems/flatten-2d-vector/ 题目: Implement an iterator to flatten a 2d v ...
- Flatten 2D Vector -- LeetCode
Implement an iterator to flatten a 2d vector. For example,Given 2d vector = [ [,], [], [,,] ] By cal ...
- [Swift]LeetCode251.展平二维向量 $ Flatten 2D Vector
Implement an iterator to flatten a 2d vector. For example,Given 2d vector = [ [1,2], [3], [4,5,6] ] ...
- Flatten 2D Vector
Implement an iterator to flatten a 2d vector. For example, Given 2d vector = [ [1,2], [3], [4,5,6] ] ...
- [Locked] Flatten 2D Vector
Problem Description: Implement an iterator to flatten a 2d vector. For example,Given 2d vector = [ [ ...
随机推荐
- python有哪些好的学习资料或者博客?
推荐Full Stack Python 有各种python资源汇总,从基础入门到各种框架web应用开发和部署,再到高级的ORM.Docker都有.以下是Full Stack Python 上总结的一些 ...
- Linux文件系统管理 常见命令df、du、fsck、dumpe2fs
概述 Linux文件系统管理 常见命令df.du.fsck.dumpe2fs. 文件系统查看命令:df 通过 df 命令可以査看已经挂载的文件系统的信息包括设备文件名.文件系统总大小.已经使用的大小. ...
- box-flex兼容写法
box-flex布局在这几年发生了多次变化,可分为2009版.2011版以及2013版, 区分: display:box(inline-box), box-{*}的格式为2009版 display:b ...
- requests.post处理Content-Type: multipart/form-data的请求
前几天遇到一个需求,要调用一个接口发送请求,抓包之后得到的数据是这样的 上网看了一些资料得知,原来这个接口的数据是通过multipart/form-data格式传过去的,multipart/form- ...
- vRO 添加已有磁盘到VM
在vRO实现将已有虚拟机磁盘添加到另外的虚拟机上,以为vRA发布Oracle/SQL集群做准备: // 脚本需要两个输入 vm_obj和diskPathSystem.log("Attempt ...
- POI实现数据的导入
1.POI技术的概述? POI技术:apache POI是可以对微软office文档进行读和写的工具. l HSSF:操作97格式的excel,扩展名:.xls 纯二进制,最大行数65535. l X ...
- 阅读linux内核代码的工具-- Source Insight
http://blog.csdn.net/luckyaslan/article/details/7869235 Step 1:安装Source Insight并启动程序 可以进入图1界面,在工具条上有 ...
- Java日期时间输出格式优化
使用printf格式化日期 printf 方法可以很轻松地格式化时间和日期.使用两个字母格式,它以 %t 开头并且以下面表格中的一个字母结尾. 转 换 符 说 明 示 例 c 包括全部 ...
- Centos 查看版本
# cat /etc/redhat-release CentOS Linux release 7.4.1708 (Core)
- python 矩阵分成上三角下三角和对角三个矩阵
diagonal Return specified diagonals. diagflat Create a 2-D array with the flattened input as a diago ...