【发布时间】:2021-12-22 13:22:06
【问题描述】:
我的 leetcode 出现错误,我不知道为什么:
var addTwoNumbers = function(l1, l2) {
let newL1 = []
let newL2 = []
let answer = []
for(let i = 0; i < l1.length; i++) {
newL1[i] = l1[l1.length - 1 - i]
}
for(let i = 0; i < l2.length; i++) {
newL2[i] = l2[l2.length - 1 - i]
}
let num = parseInt(newL1.toString().replace(/,/g, '')) + parseInt(newL2.toString().replace(/,/g, ''))
let rawAnswer = (num.toString().split(""))
for(let i=0; i < rawAnswer.length; i++) {
answer[i] = parseInt(rawAnswer[i])
}
return answer
}
错误:
Line 45 in solution.js
throw new TypeError(__serialize__(ret) + " is not valid value for the expected return type ListNode");
^
TypeError: null is not valid value for the expected return type ListNode
Line 45: Char 20 in solution.js (Object.<anonymous>)
Line 16: Char 8 in runner.js (Object.runner)
Line 29: Char 26 in solution.js (Object.<anonymous>)
Line 1251: Char 30 in loader.js (Module._compile)
Line 1272: Char 10 in loader.js (Object.Module._extensions..js)
Line 1100: Char 32 in loader.js (Module.load)
Line 962: Char 14 in loader.js (Function.Module._load)
at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:72:12)
Line 17: Char 47 in run_main_module.js
挑战说明:
给定两个代表两个非负整数的非空链表。这些数字以相反的顺序存储,并且它们的每个节点都包含一个数字。将两个数字相加并将总和作为链表返回。 你可以假设这两个数字不包含任何前导零,除了数字 0 本身。
例子:
Input: l1 = [2,4,3], l2 = [5,6,4]
Output: [7,0,8]
Explanation: 342 + 465 = 807.
我不知道为什么会出现这个错误,但我知道我正在做一些 leetcode 不喜欢的事情。
谢谢
【问题讨论】:
-
错误说它期待一个
ListNode对象。但看起来你正在返回一个数字。 -
我没有检查你的逻辑,但是当你应该将它作为数字列表返回时,你会返回一个数字(在示例中相当于 807)
-
你为什么删除旧问题并开始一个新问题?
-
@Wyck 你是对的。我刚刚修好了。问题在于最初的 for 循环。但从 let 代码返回的答案是 [nan, nan, nan]。我正在通过 vscode 运行它,我得到了正确的答案,所以我只是不确定我的语法的哪一部分导致 leetcode 关闭。
标签: javascript arrays nodes