-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0251-flatten-2d-vector.js
More file actions
36 lines (33 loc) · 912 Bytes
/
0251-flatten-2d-vector.js
File metadata and controls
36 lines (33 loc) · 912 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
/**
* Flatten 2d Vector
* Time Complexity: O(1)
* Space Complexity: O(1)
*/
var Vector2D = function (inputTwoDArray) {
this.storedTwoDVector = inputTwoDArray;
this.currentTraversalRow = 0;
this.currentTraversalColumn = 0;
};
Vector2D.prototype.preparePointers = function () {
while (
this.currentTraversalRow < this.storedTwoDVector.length &&
this.currentTraversalColumn >=
this.storedTwoDVector[this.currentTraversalRow].length
) {
this.currentTraversalRow++;
this.currentTraversalColumn = 0;
}
};
Vector2D.prototype.next = function () {
this.preparePointers();
const nextItem =
this.storedTwoDVector[this.currentTraversalRow][
this.currentTraversalColumn
];
this.currentTraversalColumn++;
return nextItem;
};
Vector2D.prototype.hasNext = function () {
this.preparePointers();
return this.currentTraversalRow < this.storedTwoDVector.length;
};