返回介绍

solution / 0200-0299 / 0251.Flatten 2D Vector / README_EN

发布于 2024-06-17 01:04:02 字数 5707 浏览 0 评论 0 收藏 0

251. Flatten 2D Vector

中文文档

Description

Design an iterator to flatten a 2D vector. It should support the next and hasNext operations.

Implement the Vector2D class:

  • Vector2D(int[][] vec) initializes the object with the 2D vector vec.
  • next() returns the next element from the 2D vector and moves the pointer one step forward. You may assume that all the calls to next are valid.
  • hasNext() returns true if there are still some elements in the vector, and false otherwise.

 

Example 1:

Input
["Vector2D", "next", "next", "next", "hasNext", "hasNext", "next", "hasNext"]
[[[[1, 2], [3], [4]]], [], [], [], [], [], [], []]
Output
[null, 1, 2, 3, true, true, 4, false]

Explanation
Vector2D vector2D = new Vector2D([[1, 2], [3], [4]]);
vector2D.next();  // return 1
vector2D.next();  // return 2
vector2D.next();  // return 3
vector2D.hasNext(); // return True
vector2D.hasNext(); // return True
vector2D.next();  // return 4
vector2D.hasNext(); // return False

 

Constraints:

  • 0 <= vec.length <= 200
  • 0 <= vec[i].length <= 500
  • -500 <= vec[i][j] <= 500
  • At most 105 calls will be made to next and hasNext.

 

Follow up: As an added challenge, try to code it using only iterators in C++ or iterators in Java.

Solutions

Solution 1

class Vector2D:
  def __init__(self, vec: List[List[int]]):
    self.i = 0
    self.j = 0
    self.vec = vec

  def next(self) -> int:
    self.forward()
    ans = self.vec[self.i][self.j]
    self.j += 1
    return ans

  def hasNext(self) -> bool:
    self.forward()
    return self.i < len(self.vec)

  def forward(self):
    while self.i < len(self.vec) and self.j >= len(self.vec[self.i]):
      self.i += 1
      self.j = 0


# Your Vector2D object will be instantiated and called as such:
# obj = Vector2D(vec)
# param_1 = obj.next()
# param_2 = obj.hasNext()
class Vector2D {
  private int i;
  private int j;
  private int[][] vec;

  public Vector2D(int[][] vec) {
    this.vec = vec;
  }

  public int next() {
    forward();
    return vec[i][j++];
  }

  public boolean hasNext() {
    forward();
    return i < vec.length;
  }

  private void forward() {
    while (i < vec.length && j >= vec[i].length) {
      ++i;
      j = 0;
    }
  }
}

/**
 * Your Vector2D object will be instantiated and called as such:
 * Vector2D obj = new Vector2D(vec);
 * int param_1 = obj.next();
 * boolean param_2 = obj.hasNext();
 */
class Vector2D {
public:
  Vector2D(vector<vector<int>>& vec) {
    this->vec = move(vec);
  }

  int next() {
    forward();
    return vec[i][j++];
  }

  bool hasNext() {
    forward();
    return i < vec.size();
  }

private:
  int i = 0;
  int j = 0;
  vector<vector<int>> vec;

  void forward() {
    while (i < vec.size() && j >= vec[i].size()) {
      ++i;
      j = 0;
    }
  }
};

/**
 * Your Vector2D object will be instantiated and called as such:
 * Vector2D* obj = new Vector2D(vec);
 * int param_1 = obj->next();
 * bool param_2 = obj->hasNext();
 */
type Vector2D struct {
  i, j int
  vec  [][]int
}

func Constructor(vec [][]int) Vector2D {
  return Vector2D{vec: vec}
}

func (this *Vector2D) Next() int {
  this.forward()
  ans := this.vec[this.i][this.j]
  this.j++
  return ans
}

func (this *Vector2D) HasNext() bool {
  this.forward()
  return this.i < len(this.vec)
}

func (this *Vector2D) forward() {
  for this.i < len(this.vec) && this.j >= len(this.vec[this.i]) {
    this.i++
    this.j = 0
  }
}

/**
 * Your Vector2D object will be instantiated and called as such:
 * obj := Constructor(vec);
 * param_1 := obj.Next();
 * param_2 := obj.HasNext();
 */
class Vector2D {
  i: number;
  j: number;
  vec: number[][];

  constructor(vec: number[][]) {
    this.i = 0;
    this.j = 0;
    this.vec = vec;
  }

  next(): number {
    this.forward();
    return this.vec[this.i][this.j++];
  }

  hasNext(): boolean {
    this.forward();
    return this.i < this.vec.length;
  }

  forward(): void {
    while (this.i < this.vec.length && this.j >= this.vec[this.i].length) {
      ++this.i;
      this.j = 0;
    }
  }
}

/**
 * Your Vector2D object will be instantiated and called as such:
 * var obj = new Vector2D(vec)
 * var param_1 = obj.next()
 * var param_2 = obj.hasNext()
 */

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文