账号密码登录
微信安全登录
微信扫描二维码登录

登录后绑定QQ、微信即可实现信息互通

手机验证码登录
找回密码返回
邮箱找回 手机找回
注册账号返回
其他登录方式
分享
  • 收藏
    X
    改成dfs(nextIndex, steps + +),为啥leetcode报错呢?
    48
    0
    var numWays = function(n, relation, k) { let ways = 0; const edges = new Array(n).fill(0).map(() => new Array()); for (const [src, dst] of relation) { edges[src].push(dst); } const dfs = (index, steps) => { if (steps === k) { if (index === n - 1) { ways++; } return; } const list = edges[index]; for (const nextIndex of list) { dfs(nextIndex, steps + 1); } } dfs(0, 0); return ways;}var relation = [[0,2],[2,1],[3,4],[2,3],[1,4],[2,0],[0,4]]numWays(5,relation,3)报错:maximum call stack size exceed不改不报错
    0
    打赏
    收藏
    点击回答
        全部回答
    • 0
    • 清酒孤欢ゞ 普通会员 1楼

      在LeetCode中,这个代码的错误在于"undefined nextIndex"。正确的应该是"undefined next"。

      正确的代码应该是:

      ```javascript function dfs(nextIndex, steps) { if (steps === 0) { return []; }

      return dfs(nextIndex + 1, steps - 1);
      

      } ```

      在这个代码中,我们使用递归函数dfs,并且在每次递归调用时,我们都会将nextIndex加1。这样,steps的值就会逐渐减小,直到它等于0,这时我们就可以停止递归。

    更多回答
    扫一扫访问手机版
    • 回到顶部
    • 回到顶部