LeetCode 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 Flatten 2D Vector的更多相关文章
- [LeetCode] Flatten 2D Vector 压平二维向量
Implement an iterator to flatten a 2d vector. For example,Given 2d vector = [ [1,2], [3], [4,5,6] ] ...
- LeetCode 251. Flatten 2D Vector
原题链接在这里:https://leetcode.com/problems/flatten-2d-vector/ 题目: Implement an iterator to flatten a 2d v ...
- [LeetCode] 251. 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 -- LeetCode
Implement an iterator to flatten a 2d vector. For example,Given 2d vector = [ [,], [], [,,] ] By cal ...
- 251. Flatten 2D Vector
题目: Implement an iterator to flatten a 2d vector. For example,Given 2d vector = [ [1,2], [3], [4,5,6 ...
- [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 = [ [ ...
- 251. Flatten 2D Vector 平铺二维矩阵
[抄题]: Implement an iterator to flatten a 2d vector. Example: Input: 2d vector = [ [1,2], [3], [4,5,6 ...
随机推荐
- 如何真正提高ASP.NET网站的性能
摘要:前言 怎么才能让asp.net网站飞得更快,有更好的性能?这是很多开发者常常思考的一个问题.我有时候会做大量的测试,或请求别人帮忙采集一些数据,希望能够验证网上一些专家的建议或证明 前言 怎么才 ...
- .htaccess
一.在Apache配置中启用Rewrite 打开配置文件httpd.conf: 1.启用rewrite# LoadModule rewrite_module modules/mod_rewrite.s ...
- UVa 11082 & 最大流的行列模型
题意: 给出一个矩阵前i行的和与前j列的和,(i∈[1,r],j属于[1,c]),每个元素ai,j∈[1,20],请你还原出这个矩阵,保证有解. SOL: 给网络流建模跪了,神一样的建图,如果我我会怎 ...
- HDU 4597 Play Game
题目链接 什么都不想说,最近状态暴跌.. #include <cstdio> #include <cstring> #include <iostream> usin ...
- Lable得到自定义高度!
方法1(系统):CGSize declabesize = [_questionDecLabel.text boundingRectWithSize:CGSizeMake(CGRectGetWidth( ...
- not only ... but also
轉載自http://210.240.55.2/~t311/moe/engb6/b6grammar/b6notonly.htm not only ... but also ... 是「不僅‧‧‧也是‧‧ ...
- JS中注意事项
(一)判断中注意事项 一.所有的相对路径都别拿来做判断 1.img src='...' 2.href='1.css', href='html/index.html' 3.img src='http:/ ...
- [LintCode] Intersection of Two Arrays 两个数组相交
Given two arrays, write a function to compute their intersection.Notice Each element in the result m ...
- web实验指导书和课后习题参考答案
实验指导书 :http://course.baidu.com/view/daf55bd026fff705cc170add.html 课后习题参考答案:http://wenku.baidu.com/li ...
- unity3d插件Daikon Forge GUI 中文教程3-基础控件Button和Sprite的使用
2.2添加一个按钮Button 来看看特有的属性:Button Properties Data 显示的文本 Behavior 中的几个: Aoto Size 选中时就是按钮的背景会根据Data中的文本 ...