Created
November 4, 2020 14:41
-
-
Save luojiyin1987/d97d4fa99ef4814a7a22b192c473d244 to your computer and use it in GitHub Desktop.
RLE 迭代器
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * @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