Flatten 2D Vector
Description
null
Solution(javascript)
/**
* 可以转成一维数组,然后迭代或者按需
* @param {number[][]} v
*/
const Vector2D = function (v) {
this.row = 0
this.column = 0
this.matrix = v
}
/**
* @return {number}
*/
Vector2D.prototype.next = function () {
if (this.row < this.matrix.length && this.column < this.matrix[this.row].length) {
const value = this.matrix[this.row][this.column]
this.column += 1
return value
}
this.row++
this.column = 0
return this.next()
}
/**
* @return {boolean}
*/
Vector2D.prototype.hasNext = function () {
if (this.row >= this.matrix.length) {
return false
}
if (this.column < this.matrix[this.row].length) {
return true
}
this.row++
this.column = 0
return this.hasNext()
}