当前位置: 首页 > 知识库问答 >
问题:

javascript - 力扣 代码执行为什么会出错呢?

武功
2023-04-29
var NestedIterator = function (nestedList) {
  function loop(arr, data) {
    if (Array.isArray(arr))
      arr.forEach((e) => {
        if (Array.isArray(e)) loop(e, data);
        else data.push(e);
      });
    else data.push(arr);
  }
  this.index = 0;
  this.Array = []; 
  loop(nestedList, this.Array);
}; 
NestedIterator.prototype.hasNext = function () {
  return this.index < this.Array.length  ;
}; 
NestedIterator.prototype.next = function () {
  if (this.index < this.Array.length  ) return this.Array[this.index++];
  return null;
};

image.png
题目地址 : https://leetcode.cn/problems/flatten-nested-list-iterator/
为什么会出错呢?

共有1个答案

赖星驰
2023-04-29
/**
 * // 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.flattenedList = [];
    this.index = 0;
    
    const flatten = (list) => {
        for (const item of list) {
            if (item.isInteger()) {
                this.flattenedList.push(item.getInteger());
            } else {
                flatten(item.getList());
            }
        }
    };
    
    flatten(nestedList);
};

/**
 * @this NestedIterator
 * @returns {boolean}
 */
NestedIterator.prototype.hasNext = function () {
    return this.index < this.flattenedList.length;
};

/**
 * @this NestedIterator
 * @returns {integer}
 */
NestedIterator.prototype.next = function () {
    return this.flattenedList[this.index++];
};

/**
 * Your NestedIterator will be called like this:
 * var i = new NestedIterator(nestedList), a = [];
 * while (i.hasNext()) a.push(i.next());
 */
 类似资料:
  • 如图,代码来自官方文档的reference,版本也是1.0.0-rc.13

  • 我运行这段代码的时候,点击部件就会出现 Uncaught TypeError: Cannot read properties of undefined (reading 'position')at VertexEnumerator.js:87:28 这样的错误

  • 问题内容: 我想知道以下代码的行为背后的机制是什么: 我的理解是不 返回 函数,而是 关闭连接/结束请求 。这可以解释为什么我仍然可以在命令后执行代码(我查看了快速源,但它似乎不是异步函数)。 还有其他我可能会想念的东西吗? 问题答案: 当然可以结束HTTP响应,但是它对您的代码没有做任何特殊的事情。 即使您已结束回复,也可以继续做其他事情。 但是,您 无法 做的是利用进行任何有用的操作。由于响应

  • react class组件在componentDidMount中调用初始化接口,有些时候会调用两次,通过断点发现顺序是componentDidMount->componentWillUnmount->componentDidMount,但不能稳定复现,调用的组件是页面的主入口,并非某个组件的子组件,请问有知道这个问题的么?

  • 我正在做一个编码练习:给定一个整数序列作为一个数组,确定是否可以通过从数组中删除不超过一个元素来获得严格递增的序列。 所以我写了这段代码: 现在,这段代码似乎适用于大多数序列,但这段代码引发了一个错误: 错误如下: 我只是不明白列表索引怎么可能超出范围…有人有线索吗?