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 = [ [ ...
随机推荐
- SpringBoot2.1.0 application.properties配置
# =================================================================== # COMMON SPRING BOOT PROPERTIE ...
- VoLTE的前世今生...说清楚VoIP、VoLTE、CSFB、VoWiFi、SIP、IMS那些事...
转:https://mp.weixin.qq.com/s?__biz=MzA3MTA3OTIwMw==&mid=401344844&idx=1&sn=497b351f524af ...
- 使用awk来提取内容
1.提取gff文件中的HLA基因的相关bed文件. gff的格式: zcat *gz|gawk 'BGIN{FS="\t";OFS="\t"}$3==" ...
- php数组函数-array_key_exists()
array_key_exists()函数判断某个数组中是否存在指定的key,如果key存 在,则返回true,否则返回flase array_key_exists(key,array); key:必需 ...
- OC_内存管理
引言: 1.OC中的对象都是分配在堆中的 声明对象的格式: Person *person = [Person new ...
- 前段时间说了AssetBundle打包,先设置AssetLabels,再执行打包,但是这样有个弊端就是所有设置了AssetLabels的资源都会打包,这次说说不设置AssetLabels,该如何打包AssetBundle
BuildPipeline.BuildAssetBundles() 这个函数,有多个重载,一个不用AssetBundleBuild数组,一个需要,如果设置了AssetLabels,那么这时候是不需要的 ...
- STL中一些函数的应用
1.nth_element():找到第几大的数.用法:nth_element(a,a+k,a+n),返回一个数组a中第k大的数,时间复杂度比较小,头文件#include <algorithm&g ...
- 解决maven寻找依赖关系失败的问题
在mac中会碰到依赖jdk自带的jar包而maven找不到的问题 解决方案:安装jdk中的tools到本地mvn库 mvn install:install-file -Dfile=${JAVA_HOM ...
- c#.NET中日志信息写入Windows日志中解决方案
1. 目的应用系统的开发和维护离不开日志系统,选择一个功能强大的日志系统解决方案是应用系统开发过程中很重要的一部分.在.net环境下的日志系统解决方案有许多种,log4net是其中的佼佼者.在Wind ...
- MinGW main()
MinGW没有wmain入口函数,为了获取宽字符的参数,可以用系统API函数GetCommandLineW. main.cpp #include <iostream> #include & ...