-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy path0281-zigzag-iterator.js
More file actions
49 lines (44 loc) · 1.21 KB
/
0281-zigzag-iterator.js
File metadata and controls
49 lines (44 loc) · 1.21 KB
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
/**
* 281. Zigzag Iterator
* https://leetcode.com/problems/zigzag-iterator/
* Difficulty: Medium
*
* Given two vectors of integers v1 and v2, implement an iterator to return their elements
* alternately.
*
* Implement the ZigzagIterator class:
* - ZigzagIterator(List<int> v1, List<int> v2) initializes the object with the two vectors
* v1 and v2.
* - boolean hasNext() returns true if the iterator still has elements, and false otherwise.
* - int next() returns the current element of the iterator and moves the iterator to the
* next element.
*/
/**
* @constructor
* @param {Integer[]} v1
* @param {Integer[]} v2
*/
var ZigzagIterator = function ZigzagIterator(v1, v2) {
this.queue = [];
if (v1.length) this.queue.push([v1, 0]);
if (v2.length) this.queue.push([v2, 0]);
};
/**
* @this ZigzagIterator
* @returns {boolean}
*/
ZigzagIterator.prototype.hasNext = function hasNext() {
return this.queue.length > 0;
};
/**
* @this ZigzagIterator
* @returns {integer}
*/
ZigzagIterator.prototype.next = function next() {
const [vector, index] = this.queue.shift();
const value = vector[index];
if (index + 1 < vector.length) {
this.queue.push([vector, index + 1]);
}
return value;
};