forked from paopao2/leetcode-js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFlatten 2D Vector.js
78 lines (62 loc) · 1.69 KB
/
Flatten 2D Vector.js
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
/**
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 you need to keep track?
Two variables is all you need. Try with x and y.
Beware of empty rows. It could be the first few rows.
To write correct code, think about the invariant to maintain. What is it?
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?
Not sure? Think about how you would implement hasNext(). Which is more complex?
Common logic in two different places should be refactored into a common method.
*/
/**
* @constructor
* @param {Integer[][]} vec2d
*/
var Vector2D = function(vec2d) {
this.vec = vec2d;
this.row = 0;
this.col = 0;
};
/**
* @this Vector2D
* @returns {boolean}
*/
Vector2D.prototype.hasNext = function() {
if (this.vec.length === 0) {
return false;
}
if (this.col === this.vec[this.row].length) {
this.col = 0;
this.row++;
}
while (this.row < this.vec.length && this.vec[this.row].length === 0) {
this.row++;
}
if (this.row === this.vec.length) {
return false;
}
return true;
};
/**
* @this Vector2D
* @returns {integer}
*/
Vector2D.prototype.next = function() {
const val = this.vec[this.row][this.col];
this.col++;
return val;
};
/**
* Your Vector2D will be called like this:
* var i = new Vector2D(vec2d), a = [];
* while (i.hasNext()) a.push(i.next());
*/