251. 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].
[暴力解法]:
时间分析:
空间分析:
[优化后]:
时间分析:
空间分析:
[奇葩输出条件]:
[奇葩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 平铺二维矩阵的更多相关文章
- [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 ...
- [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 ...
- [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 ...
- [LeetCode] 251. Flatten 2D Vector 压平二维向量
Implement an iterator to flatten a 2d vector. For example,Given 2d vector = [ [1,2], [3], [4,5,6] ] ...
- 251. 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 ...
- 240 Search a 2D Matrix II 搜索二维矩阵 II
编写一个高效的算法来搜索 m x n 矩阵中的一个目标值.该矩阵具有以下特性: 每行的元素从左到右升序排列. 每列的元素从上到下升序排列.例如,考虑下面的矩阵:[ [1, 4, 7 ...
- Leetcode240. Search a 2D Matrix II搜索二维矩阵2
编写一个高效的算法来搜索 m x n 矩阵 matrix 中的一个目标值 target.该矩阵具有以下特性: 每行的元素从左到右升序排列. 每列的元素从上到下升序排列. 示例: 现有矩阵 matrix ...
- 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 ...
随机推荐
- 批量将某一目录下的.py文件改为.txt格式文件
#!/usr/env/python#-*- coding:utf-8 -*-#批量将某一目录下的.py文件改为.txt格式文件import os,os.pathfile_list = os.listd ...
- Devexpress Winform MVVM
归纳总结备忘 Devexpress Winform MVVM Practice 前言 MVVM Devexpress 正文 databindings及 UI Triggers Command 委托Co ...
- caffe-windows环境配置(github上官方BVLC/caffe的推荐配置方法详解)
[转载来的文章:如有侵权,请联系我!我将马上删除!] 首先声明一下,如标题,本教程是caffe在windows系统上的配置方法,而且是github上官方BVLC/caffe目前推荐的配置方法,并不是使 ...
- JAVA AES CBC 加密 解密
AES 256 , KEY 的长度为 32字节(32*8=256bit). AES 128 , KEY 的长度为 16字节(16*8=128bit) CBC 模式需要IV, IV的值是固定写死,还是当 ...
- NPOI导出excel(2.0.6版本)
public static void WriteExcel(System.Data.DataTable dt,string fileName) { NPOI.XSSF.UserModel.XSSFWo ...
- The 'INFORMATION_SCHEMA.GLOBAL_STATUS' feature is disabled; see the documentation for 'show_compatibility_56'
--从mysql5.7.6开始information_schema.global_status已经开始被舍弃,为了兼容性,此时需要打开 show_compatibility_56 mysql> ...
- x86 asm调用框架(vs2015)
EXTERN_C void _stdcall Asm_1();//在cpp文件下 要使用EXTERN_C . .MODEL FLAT,C,stdcall .DATA .CODE Asm_1 PROC ...
- springboot 的war包在Tomcat中启动失败
springboot 默认是通常是打包成jar的,里面会内置一个tomcat容器 有时候我们需要使用以前打成war包的方式部署到对应的tomcat中, 具体springboot 怎么从jar改成war ...
- [UE4]Spin Box,数字输入,可拖动
一.Spin Box在Input组下 二.Spin Box的文字样式可以在Spin Box.Display中修改 三.Spin Box事件 1.On Value Changed:值改变时触发 2.On ...
- 【3D美术教程】手雷(传统与PBR流程)
转自:https://www.sohu.com/a/156489635_718614 随着最新的次时代技术PBR流程的普及,越来越多的公司由传统流程转向了PBR流程,主要原因在于PBR材质不仅效果上更 ...