Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions leetcode3/정진영/284. Peeking Iterator
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/**
* // This is the Iterator's API interface.
* // You should not implement it, or speculate about its implementation.
* function Iterator() {
* @ return {number}
* this.next = function() { // return the next number of the iterator
* ...
* };
*
* @return {boolean}
* this.hasNext = function() { // return true if it still has numbers
* ...
* };
* };
*/

/**
* @param {Iterator} iterator
*/
var PeekingIterator = function(iterator) {
this.iterator = iterator;

this.nextValue = iterator.hasNext() ? iterator.next() : null;
};

/**
* @return {number}
*/
PeekingIterator.prototype.peek = function() {
return this.nextValue;
};

/**
* @return {number}
*/
PeekingIterator.prototype.next = function() {
const ans = this.nextValue;

this.nextValue = this.iterator.hasNext() ? this.iterator.next() : null;
return ans;
};

/**
* @return {boolean}
*/
PeekingIterator.prototype.hasNext = function() {
return this.nextValue !== null;
};

/**
* Your PeekingIterator object will be instantiated and called as such:
* var obj = new PeekingIterator(arr)
* var param_1 = obj.peek()
* var param_2 = obj.next()
* var param_3 = obj.hasNext()
*/
Loading