Flatten 2D Vector
Total Accepted: 8352 Total Submissions: 24915 Difficulty: Medium
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].
思路
public class Vector2D {
Iterator<List<Integer>> it;
Iterator<Integer> curr;
public Vector2D(List<List<Integer>> vec2d) {
it = vec2d.iterator();
}
public int next() {
hasNext();
return curr.next();
}
public boolean hasNext() {
while((curr == null || !curr.hasNext()) && it.hasNext()){
curr = it.next().iterator();
}
return curr != null && curr.hasNext();
}
}
后来写的,在hasNext()里边进行操作
- 注意i.next()的类型转化
- 或者在声明的时候
- Iterator
> it;
- Iterator curr;
- 而不是简单Iterator
- 每当i2是null的时候,就是还没赋值,或者说,i1有,但是i2已经为空,就需要进入下一轮迭代,
- 但是有可能出现[]这样的空集,这会i2还是为空,所以要用while
public class Vector2D implements Iterator<Integer> {
private Iterator i1;
private Iterator i2;
public Vector2D(List<List<Integer>> vec2d) {
i1 = vec2d.iterator();
}
@Override
public Integer next() {
return (Integer)i2.next();
}
@Override
public boolean hasNext() {
while ((i2 == null || !i2.hasNext()) && i1.hasNext()) {
List<Integer> list = (List<Integer>)i1.next();
i2 = list.iterator();
}
if (i2 == null) {
return false;
}
return i1.hasNext() || i2.hasNext();
}
}