Skip to content

Instantly share code, notes, and snippets.

@luojiyin1987
Created November 4, 2020 14:41
Show Gist options
  • Save luojiyin1987/d97d4fa99ef4814a7a22b192c473d244 to your computer and use it in GitHub Desktop.
Save luojiyin1987/d97d4fa99ef4814a7a22b192c473d244 to your computer and use it in GitHub Desktop.
RLE 迭代器
/**
* @param {number[]} A
*/
var RLEIterator = function(A) {
this.data =A;
this.count = this.data.shift()||0;
this.num = this.data.shift();
};
/**
* @param {number} n
* @return {number}
*/
RLEIterator.prototype.next = function(n) {
this.count -= n;
while(this.count<0 && this.data.length>0){
this.count += this.data.shift();
this.num = this.data.shift();
}
if(this.count <0) {
return -1;
}else {
return this.num;
}
};
/**
* Your RLEIterator object will be instantiated and called as such:
* var obj = new RLEIterator(A)
* var param_1 = obj.next(n)
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment