12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182 |
- Array.prototype.peek = function() {
- return this[this.length - 1]
- }
- var NestedIterator = function(nestedList) {
- this.st = [nestedList]
- this.idx = [0]
- this.findNext()
- }
- NestedIterator.prototype.hasNext = function() {
- return this.idx.length !== 0
- }
- NestedIterator.prototype.next = function() {
- let val = this.st.peek()[this.idx.peek()].getInteger()
- this.idx[this.idx.length-1]++
- this.findNext()
- return val
- }
- NestedIterator.prototype.findNext = function() {
- while (this.idx.length !== 0) {
- if (this.idx.peek() == this.st.peek().length) {
- this.idx.pop()
- this.st.pop()
- if (this.st.length !== 0) {
- this.idx[this.idx.length-1]++
- }
- } else if (!this.st.peek()[this.idx.peek()].isInteger()) {
- this.st.push(this.st.peek()[this.idx.peek()].getList())
- this.idx.push(0)
- } else {
- break
- }
- }
- }
|