251. Flatten 2D Vector
题目:
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].
Hint:
- How many variables do you need to keep track?
- Two variables is all you need. Try with
xandy. - Beware of empty rows. It could be the first few rows.
- To write correct code, think about the invariant to maintain. What is it?
- The invariant is
xandymust always point to a valid point in the 2d vector. Should you maintain your invariant ahead of time or right when you need it? - Not sure? Think about how you would implement
hasNext(). Which is more complex? - Common logic in two different places should be refactored into a common method.
Follow up:
As an added challenge, try to code it using only iterators in C++ or iterators in Java.
链接: http://leetcode.com/problems/flatten-2d-vector/
题解:
构造一个2D的iterator。不太理解iterator的原理,第一想法是把vec2d里面的元素都读到Queue里 ,然后再逐个读取。这样的话初始化需要O(n), next和hasNext都为O(1),Space Complexity也是O(n),虽然能ac,但是当vec2d足够大的时候会出问题。
Time Complexity - constructor - O(n), hasNext - O(1), next() - O(1), Space Complexity - O(n)。
public class Vector2D {
private Queue<Integer> vec1d;
public Vector2D(List<List<Integer>> vec2d) {
vec1d = new LinkedList<>();
for(List<Integer> list : vec2d) {
for(int i : list) {
vec1d.offer(i);
}
}
}
public int next() {
if(hasNext())
return vec1d.poll();
else
return Integer.MAX_VALUE;
}
public boolean hasNext() {
return vec1d.size() > 0;
}
}
/**
* Your Vector2D object will be instantiated and called as such:
* Vector2D i = new Vector2D(vec2d);
* while (i.hasNext()) v[f()] = i.next();
*/
Update: 保存两个变量来遍历vec2d
public class Vector2D {
private List<List<Integer>> list;
private int listIndex;
private int elemIndex;
public Vector2D(List<List<Integer>> vec2d) {
list = vec2d;
listIndex = 0;
elemIndex = 0;
}
public int next() {
return list.get(listIndex).get(elemIndex++);
}
public boolean hasNext() {
while(listIndex < list.size()) {
if(elemIndex < list.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();
*/
二刷:
使用了ArrayList的iterator,这个算不算作弊...思路就是,一开始把vec2d里面每个list的iterator都加入到一个iters的ArrayList里。之后就可以很简单地写好hasNext()和next()两个方法了。
Java:
public class Vector2D implements Iterator<Integer> {
private List<Iterator<Integer>> iters;
private int curLine = 0;
public Vector2D(List<List<Integer>> vec2d) {
this.iters = new ArrayList<>();
for (List<Integer> list : vec2d) {
iters.add(list.iterator());
}
}
@Override
public Integer next() {
return iters.get(curLine).next();
}
@Override
public boolean hasNext() {
while (curLine < iters.size()) {
if (iters.get(curLine).hasNext()) return true;
else curLine++;
}
return false;
}
}
/**
* Your Vector2D object will be instantiated and called as such:
* Vector2D i = new Vector2D(vec2d);
* while (i.hasNext()) v[f()] = i.next();
*/
Update:
还是使用两个元素来遍历
public class Vector2D implements Iterator<Integer> {
private List<List<Integer>> list;
private int curLine = 0;
private int curElem = 0;
public Vector2D(List<List<Integer>> vec2d) {
this.list = vec2d;
}
@Override
public Integer next() {
return list.get(curLine).get(curElem++);
}
@Override
public boolean hasNext() {
while (curLine < list.size()) {
if (curElem < list.get(curLine).size()) {
return true;
} else {
curLine++;
curElem = 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();
*/

Reference:
http://web.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/17a.Iterators.pdf
http://docs.oracle.com/javase/7/docs/api/
http://stackoverflow.com/questions/21988341/how-to-iterate-through-two-dimensional-arraylist-using-iterator
http://www.cs.cornell.edu/courses/cs211/2005fa/Lectures/L15-Iterators%20&%20Inner%20Classes/L15cs211fa05.pdf
https://leetcode.com/discuss/50292/7-9-lines-added-java-and-c-o-1-space
https://leetcode.com/discuss/55199/pure-iterator-solution-additional-data-structure-list-get
https://leetcode.com/discuss/57984/simple-and-short-java-solution-with-iterator
https://leetcode.com/discuss/50356/my-concise-java-solution
https://leetcode.com/discuss/68860/java-o-1-space-solution
https://leetcode.com/discuss/71002/java-solution-beats-60-10%25
251. Flatten 2D Vector的更多相关文章
- 251. Flatten 2D Vector 平铺二维矩阵
[抄题]: Implement an iterator to flatten a 2d vector. Example: Input: 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] ] ...
- [LeetCode] 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] ] ...
- LeetCode Flatten 2D Vector
原题链接在这里:https://leetcode.com/problems/flatten-2d-vector/ 题目: Implement an iterator to flatten a 2d v ...
- [Locked] Flatten 2D Vector
Problem Description: Implement an iterator to flatten a 2d vector. For example,Given 2d vector = [ [ ...
- [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 -- LeetCode
Implement an iterator to flatten a 2d vector. For example,Given 2d vector = [ [,], [], [,,] ] By cal ...
随机推荐
- nginx服务器绑定域名和设置根目录
首先进入nginx安装目录的配置目录conf,然后执行 vi conf/nginx.conf 打开nginx的配置文件,找到并修改红字部分 server { listen default_server ...
- Basic Operation about Linux
1. 永久开启/关闭防火墙 在linux中防火墙是一个名叫iptables的工具 开启: chkconfig iptables on 关闭: chkconfig iptables off 即时生效,重 ...
- UnitTest
using Bll; using Model; using Dal; using NUnit.Framework; using NUnit.Mocks; using System.ServiceMod ...
- Android手机做无线中继路由器
为什么要拿手机做路由器?因为我现在每天要带着一个火柴盒大小的路由器(703n).它提供了一个f了q的无线网络,电脑,手机,平板等设备连接上这个无线网络之后之后就可以自由上twitter,看youtub ...
- 结构体,公用体,枚举类型的sizeof
1)枚举类enum型空间计算 enum只是定义了一个常量集合,里面没有“元素”,而枚举类型是当做int来存储的,所以枚举类型的sizeof值都为4 enum color(red,pink,white, ...
- Ha ha lou!
忙了一个晚上,终于稍微把这个模板修缮好了一点=-=//,然而我并不知道怎么像别的大牛一样,博客跟自己做的页面一样.总之今天就先到这里啦! 我的QQ是270115270,不知道会不会有人来呢=-=. ( ...
- 修复jquery.treeview的增加子节点的方法的bug
1.修复理由 在一个android项目中用到了treeview控件(本来自己通过android的原生api实现了一个http://www.cnblogs.com/Mr-Nobody/p/3527688 ...
- Eclipse中创建标准web工程以及标准目录结构说明
最近公司有个Web项目,项目结构如下: 虽然运行没有错,但是实在是别扭,标准的web应用一般不采用这种结构: 因此总结一下: 1.如何在Eclipse中创建一个标准的Web应用. 2. ...
- Codeforces Round #241 (Div. 2)->A. Guess a number!
A. Guess a number! time limit per test 1 second memory limit per test 256 megabytes input standard i ...
- Ignore files which are already versioned
If you accidentally added some files which should have been ignored, how do you get them out of vers ...