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());
  }
}

原文地址:https://www.cnblogs.com/immiao0319/p/9452882.html

时间: 2024-10-07 09:07:45

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

【LeetCode-面试算法经典-Java实现】【074-Search a 2D Matrix(搜索二维矩阵)】

[074-Search a 2D Matrix(搜索二维矩阵)] [LeetCode-面试算法经典-Java实现][所有题目目录索引] 原题 Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties: Integers in each row are sorted from left to right. The first

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

Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties: Integers in each row are sorted from left to right. The first integer of each row is greater than the last integer of the previous ro

lintcode 中等题:search a 2d matrix II 搜索二维矩阵II

题目 搜索二维矩阵 II 写出一个高效的算法来搜索m×n矩阵中的值,返回这个值出现的次数. 这个矩阵具有以下特性: 每行中的整数从左到右是排序的. 每一列的整数从上到下是排序的. 在每一行或每一列中没有重复的整数. 样例 考虑下列矩阵: [     [1, 3, 5, 7],     [2, 4, 7, 8],     [3, 5, 9, 10] ] 给出target = 3,返回 2 挑战 要求O(m+n) 时间复杂度和O(1) 额外空间 解题 直接遍历,时间复杂度是O(MN) public

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 y

[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 following properties: Integers in each row are sorted from left to right. The first integer of each row is greater than the last integer of the previous ro

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

编写一个高效的算法来搜索 m x n 矩阵中的一个目标值.该矩阵具有以下特性:    每行的元素从左到右升序排列.    每列的元素从上到下升序排列.例如,考虑下面的矩阵:[  [1,   4,  7, 11, 15],  [2,   5,  8, 12, 19],  [3,   6,  9, 16, 22],  [10, 13, 14, 17, 24],  [18, 21, 23, 26, 30]]给定目标值 target = 5, 返回 true.给定目标值 target = 20, 返回

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

Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties: Integers in each row are sorted in ascending from left to right. Integers in each column are sorted in ascending from top to bottom.

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

Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties: Integers in each row are sorted in ascending from left to right. Integers in each column are sorted in ascending from top to bottom.

转:用STL中的vector动态开辟二维数组

用STL中的vector动态开辟二维数组 源代码:#include <iostream>#include <vector>using namespace std;int main(){ int m, //行数     n; //列数 cout << "input value for m,n:"; cin>>m>>n;  //注意下面这一行:vector<int后两个">"之间要有空格!否则会被认