[抄题]:

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].

[暴力解法]:

时间分析:

空间分析:

[优化后]:

时间分析:

空间分析:

[奇葩输出条件]:

[奇葩corner case]:

j必须非空且有下才行j != null && j.hasNext();,空而有下不行。

[思维问题]:

[英文数据结构或算法,为什么不用别的数据结构或算法]:

Iterator<List<Integer>>是已有的api,可以直接拿来用。包括了next hasnext remove方法。

i = j.next().iterator();对双重数组调用.iterator()可以产生一种遍历的数据结构。

[一句话思路]:

[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):

[画图]:

[一刷]:

[二刷]:

[三刷]:

[四刷]:

[五刷]:

[五分钟肉眼debug的结果]:

[总结]:

i = j.next().iterator();对双重数组调用.iterator()可以产生一种遍历的数据结构。

[复杂度]:Time complexity: O(n) Space complexity: O(n)

[算法思想:迭代/递归/分治/贪心]:

[关键模板化代码]:

双重list的写法:

List<List<Integer>> vec2d = new ArrayList<>();
vec2d.add(Arrays.asList(1, 2));
vec2d.add(Arrays.asList(3));
vec2d.add(Arrays.asList(4, 5, 6));
Vector2D rst = new Vector2D(vec2d);

[其他解法]:

[Follow Up]:

[LC给出的题目变变变]:

[代码风格] :

[是否头一次写此类driver funcion的代码] :

[潜台词] :

// package whatever; // don't place package name!

import java.util.*;
import java.lang.*; class Vector2D { private Iterator<List<Integer>> i;
private Iterator<Integer> j; public Vector2D(List<List<Integer>> vec2d) {
i = vec2d.iterator();
} public int next() {
hasNext();
return j.next();
} public boolean hasNext() {
while ((j == null || !j.hasNext()) && i.hasNext())
j = i.next().iterator();
return j != null && j.hasNext();
}
} class MyCode {
public static void main (String[] args) {
List<List<Integer>> vec2d = new ArrayList<>();
vec2d.add(Arrays.asList(1, 2));
vec2d.add(Arrays.asList(3));
vec2d.add(Arrays.asList(4, 5, 6));
Vector2D rst = new Vector2D(vec2d);
System.out.println(rst.next());
System.out.println(rst.next());
System.out.println(rst.next());
System.out.println(rst.next());
System.out.println(rst.next());
}
}

251. Flatten 2D Vector 平铺二维矩阵的更多相关文章

  1. [CareerCup] 11.6 Search a 2D Matrix 搜索一个二维矩阵

    11.6 Given an M x N matrix in which each row and each column is sorted in ascending order, write a m ...

  2. [LeetCode] Search a 2D Matrix 搜索一个二维矩阵

    Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the follo ...

  3. [LeetCode] 74. Search a 2D Matrix 搜索一个二维矩阵

    Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the follo ...

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

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

  5. 251. Flatten 2D Vector

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

  6. LeetCode 251. Flatten 2D Vector

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

  7. 240 Search a 2D Matrix II 搜索二维矩阵 II

    编写一个高效的算法来搜索 m x n 矩阵中的一个目标值.该矩阵具有以下特性:    每行的元素从左到右升序排列.    每列的元素从上到下升序排列.例如,考虑下面的矩阵:[  [1,   4,  7 ...

  8. Leetcode240. Search a 2D Matrix II搜索二维矩阵2

    编写一个高效的算法来搜索 m x n 矩阵 matrix 中的一个目标值 target.该矩阵具有以下特性: 每行的元素从左到右升序排列. 每列的元素从上到下升序排列. 示例: 现有矩阵 matrix ...

  9. LeetCode 74. Search a 2D Matrix(搜索二维矩阵)

    Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the follo ...

随机推荐

  1. iis发布----在XP中发布高版本web遇到问题总结

    解决方法是之前在网上看到的,总结一下. 我在vs2010中做的web网页,放到XP系统中发布是遇到问题,XP中的iis版本是iis. 提示.net framework版本上的问题. 1.在网站属性中把 ...

  2. Android 异步下载

    package com.example.demo1; import java.io.File; import java.io.FileOutputStream; import java.io.IOEx ...

  3. Python shutil 模块

    高级的文件.文件夹.压缩包 处理模块 http://www.cnblogs.com/wupeiqi/articles/4963027.html

  4. PostgreSQL Oracle 兼容性之 - sys_guid()

    Oracle 使用sys_guid()用来产生UUID值.  在PostgreSQL中有类似的函数,需要安装uuid-ossp插件.  如果用户不想修改代码,还是需要使用sys_guid()函数的话, ...

  5. nginx upstream轮询配置

    nginx upstream nginx的upstream官方地址为:http://nginx.org/cn/docs/http/ngx_http_upstream_module.html 轮询分为多 ...

  6. mac os high sierra下搭建php多版本-php5.2+php5.6-nginx

    xampp的apache彻底启动不来了. php52的编译参数 ./configure --prefix=/usr/local/Cellar/php52bysk/ --with-config-file ...

  7. Python的socket

    第一部分socket的简单示例 服务器部分: """ Description: Author:Nod Date: Record: #------------------- ...

  8. 02-模拟Junit4功能

    package com.day2; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; im ...

  9. webpy 解决中文出现UnicodeDecodeError: 'ascii' codec can't decode byte 问题

    1.问题描述:一个在Django框架下使用Python编写的定时更新项目,在Windows系统下测试无误,在Linux系统下测试,报如下错误: ascii codec can't decode byt ...

  10. JavaSE中的小知识点分析

    1.System.out.println(); 调用System类中的public static final PrintStream out,输出为PrintStream(字节形式的输出流,为Outp ...