原题链接在这里: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的更多相关文章

  1. [LeetCode] 251. Flatten 2D Vector 压平二维向量

    Implement an iterator to flatten a 2d vector. For example,Given 2d vector = [ [1,2], [3], [4,5,6] ] ...

  2. 251. Flatten 2D Vector

    题目: Implement an iterator to flatten a 2d vector. For example,Given 2d vector = [ [1,2], [3], [4,5,6 ...

  3. 251. Flatten 2D Vector 平铺二维矩阵

    [抄题]: Implement an iterator to flatten a 2d vector. Example: Input: 2d vector = [ [1,2], [3], [4,5,6 ...

  4. [LeetCode] Flatten 2D Vector 压平二维向量

    Implement an iterator to flatten a 2d vector. For example,Given 2d vector = [ [1,2], [3], [4,5,6] ] ...

  5. LeetCode Flatten 2D Vector

    原题链接在这里:https://leetcode.com/problems/flatten-2d-vector/ 题目: Implement an iterator to flatten a 2d v ...

  6. Flatten 2D Vector -- LeetCode

    Implement an iterator to flatten a 2d vector. For example,Given 2d vector = [ [,], [], [,,] ] By cal ...

  7. [Swift]LeetCode251.展平二维向量 $ Flatten 2D Vector

    Implement an iterator to flatten a 2d vector. For example,Given 2d vector = [ [1,2], [3], [4,5,6] ] ...

  8. Flatten 2D Vector

    Implement an iterator to flatten a 2d vector. For example, Given 2d vector = [ [1,2], [3], [4,5,6] ] ...

  9. [Locked] Flatten 2D Vector

    Problem Description: Implement an iterator to flatten a 2d vector. For example,Given 2d vector = [ [ ...

随机推荐

  1. php基础知识测试总结

    1.LAMP具体结构包括Linux系统,Apache服务器,MySQL数据库,PHP语言. WAMP具体结构包括Windows系统,Apache服务器,MySQL数据库,PHP语言. 2.B/S架构: ...

  2. Linux常用指令——周琛

    ps ax | grep java 查看进程命令里带“java”字样的进程信息,第一列是进程号 kill -9 1234 强制杀死1234号进程 cd /xxx/xxx 进入/xxx/xxx目录 cd ...

  3. 恢复性训练day1

    DP: 0/1背包一个常见的错误是没有cmax(f[i][j],f[i-1][j]) 0/1背包的拓展中有转移式的变形,以及无限数量背包,分组背包等. 可化为背包问题的一般不会太难. 数组开小,出现大 ...

  4. 网络最大流的(Edmond Karp)算法

    容量网络:在有向图D=(V,A),指定一个点为发点,记作 s,指定另一个点为收点,记作 t,其余点叫作中间点.对于A的每条弧(Vi,Ai),都对应一个权数 C ≥0,称为弧(Vi , Ai)的容量,将 ...

  5. SpringMVC实现AJax以及RestFull风格

    RestFull风格就是url路径中不能出现?不能带参数,如https://www.baidu.com/user/item/1234这个格式,也叫url资源定位 1.需要在web.xml中开启put, ...

  6. easyui,datagrid 分页,跨域访问数据

    http://blog.itpub.net/30980622/viewspace-2051035/ 思路: 1.通过配置属性,loader加载跨域资源 2.获得$(pager).pagination对 ...

  7. linux基础(4)-常用命令

    常用命令ls ls #查看当前目录下的文件和目录 ls -l #显示详细信息 ls -a #显示所有文件 ls -t #按修改时间排序 ls -S #按文件大小排序   常用命令pwd pwd #显示 ...

  8. ggplot笔记002——qplot()函数

    qplot()函数 一年前就听说过ggplot,很多人都说ggplot强大,ggplot无所不能,从今天开始就让我们一起来见证一下这个神奇的R包. 首先要加载ggplot2: 1 if(!suppre ...

  9. 测试人员git常用命令

    首先要到git官网下载一个gitbash,并安装. https://git-scm.com/ 1. 配置git key $ ssh-keygen -t rsa 按3个回车,密码为空,在目录C:\Use ...

  10. SQL SERVER 日志已满的处理方法 (转)

    事务日志文件Transaction Log File是用来记录数据库更新情况的文件,扩展名为ldf.在 SQL Server 7.0 和 SQL Server 2000 中,如果设置了自动增长功能,事 ...