Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * // This is the interface that allows for creating nested lists.
- * // You should not implement it, or speculate about its implementation
- * function NestedInteger() {
- *
- * Return true if this NestedInteger holds a single integer, rather than a nested list.
- * @return {boolean}
- * this.isInteger = function() {
- * ...
- * };
- *
- * Return the single integer that this NestedInteger holds, if it holds a single integer
- * Return null if this NestedInteger holds a nested list
- * @return {integer}
- * this.getInteger = function() {
- * ...
- * };
- *
- * Return the nested list that this NestedInteger holds, if it holds a nested list
- * Return null if this NestedInteger holds a single integer
- * @return {NestedInteger[]}
- * this.getList = function() {
- * ...
- * };
- * };
- */
- /**
- * @constructor
- * @param {NestedInteger[]} nestedList
- */
- var NestedIterator = function(nestedList) {
- };
- /**
- * @this NestedIterator
- * @returns {boolean}
- */
- NestedIterator.prototype.hasNext = function() {
- };
- /**
- * @this NestedIterator
- * @returns {integer}
- */
- NestedIterator.prototype.next = function() {
- };
- /**
- * Your NestedIterator will be called like this:
- * var i = new NestedIterator(nestedList), a = [];
- * while (i.hasNext()) a.push(i.next());
- */
- var NestedIterator = function(nestedList) {
- var flattenArray = function(nestedList, list) {
- for (var i = 0; i < nestedList.length; i++) {
- if (nestedList[i].isInteger()) {
- list.push(nestedList[i].getInteger());
- } else {
- flattenArray(nestedList[i].getList(), list);
- }
- }
- };
- this.list = [];
- this.index = -1;
- flattenArray(nestedList, this.list);
- };
- NestedIterator.prototype.hasNext = function() {
- return this.index < this.list.length - 1;
- };
- NestedIterator.prototype.next = function() {
- this.index++;
- return this.list[this.index];
- };
Add Comment
Please, Sign In to add comment