zoukankan      html  css  js  c++  java
  • Leetcode: 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].

    I think the hint provided by Leetcode is good:

    Hint:

    1. How many variables do you need to keep track?
    2. Two variables is all you need. Try with x and y
    3. Beware of empty rows. It could be the first few rows.
    4. To write correct code, think about the invariant to maintain. What is it?
    5. The invariant is x and y must always point to a valid point in the 2d vector. Should you maintain your invariant ahead of time or right when you need it?
    6. Not sure? Think about how you would implement hasNext(). Which is more complex?
    7. Common logic in two different places should be refactored into a common method.

    So we maintain a set of (x, y), where they point to a valid none empty point in this 2d vector, and this point is also the next element that is going to return. When initializing, set (x, y) to be the first position of a none empty valid point. Every time after calling Next() function, set it to be the next valid position. y++ or y=0 when switch rows. Continue the process until x reaches vec.size()

     1 public class Vector2D {
     2     List<List<Integer>> vec;
     3     int len;
     4     int x;
     5     int y;
     6 
     7     public Vector2D(List<List<Integer>> vec2d) {
     8         this.vec = vec2d;
     9         this.len = vec.size();
    10         int i = 0;
    11         while (i<len && vec.get(i).size() == 0) {
    12             i++;
    13         }
    14         this.x = i;
    15         this.y = 0;
    16     }
    17 
    18     public int next() {
    19         int result = Integer.MAX_VALUE;
    20         if (hasNext()) {
    21             result = vec.get(x).get(y);
    22             if (y < vec.get(x).size()-1) y++;
    23             else {
    24                 x++;
    25                 while (x<vec.size() && vec.get(x).size()==0) {
    26                     x++;
    27                 }
    28                 y = 0;
    29             }
    30         }
    31         return result;
    32     }
    33 
    34     public boolean hasNext() {
    35         return (x<len)? true : false;
    36     }
    37 }
    38 
    39 /**
    40  * Your Vector2D object will be instantiated and called as such:
    41  * Vector2D i = new Vector2D(vec2d);
    42  * while (i.hasNext()) v[f()] = i.next();
    43  */
  • 相关阅读:
    centos出现“FirewallD is not running”怎么办
    百度编辑器(Ueditor)最新版(1.4.3.3)插入锚点失败原因分析及BUG修复
    centos rm -rf 恢复删除的文件
    php实现粘贴截图并完成上传功能
    微信网页授权java实现
    JAVA使用POI读取EXCEL文件的简单model
    java读取excel文件数据
    java文件操作(读流)
    oracle 10g正则表达式 REGEXP_LIKE 用法
    Oracle正则表达式函数:regexp_like、regexp_substr、regexp_instr、regexp_replace
  • 原文地址:https://www.cnblogs.com/EdwardLiu/p/5065501.html
Copyright © 2011-2022 走看看