Javascript 嵌套列表权重和算法问题

Javascript 嵌套列表权重和算法问题,javascript,arrays,multidimensional-array,Javascript,Arrays,Multidimensional Array,我试图解决“嵌套列表权重和”问题: 问题: 给定列表[[1,1],2[1,1]],返回10。(深度2处有四个1,深度2处有一个2 深度1) 这是我的解决方案 var depthSum = function (nestedList, sum=0, depth=1) { nestedList.forEach((val) => { if (Array.isArray(val)) { depth = depth+1; retu

我试图解决“嵌套列表权重和”问题:

问题:

给定列表[[1,1],2[1,1]],返回10。(深度2处有四个1,深度2处有一个2 深度1)

这是我的解决方案

var depthSum = function (nestedList, sum=0, depth=1) {
    nestedList.forEach((val) => {
        if (Array.isArray(val)) {
            depth = depth+1;
            return depthSum(val, sum, depth);
        } else {
            sum += val * depth;
        }
    });
    return sum;
};
不知道我错过了什么。当我调试它时,在某个点上我得到了答案,但是
returnsum
没有启动,我得到了一个不同的答案

有人能告诉我这个错误吗?

您可以通过返回每个级别的总和来使用和省略每个级别的总和

函数深度和(嵌套列表,级别=1){
返回nestedList.reduce((总和,值)=>
总和+(数组.isArray(val)
?深度总和(val,级别+1)
:level*val),
0);
};
log(depthSum([[1,1],2[1,1]])您可以通过返回每个级别的总和来使用和省略每个级别的
总和

函数深度和(嵌套列表,级别=1){
返回nestedList.reduce((总和,值)=>
总和+(数组.isArray(val)
?深度总和(val,级别+1)
:level*val),
0);
};

log(depthSum([[1,1],2[1,1]])所以有一种解决方法

从您的
forEach
内部返回没有任何意义,您应该做的是将递归调用中的总计添加到当前总计中。既然这样做了,就不需要将
sum
作为
depthSum
函数的参数

var nestedList=[[1,1],2[1,1]];
var depthSum=函数(嵌套列表,深度=1){
var总和=0;
nestedList.forEach((val)=>{
if(数组isArray(val)){
总和+=深度总和(值,深度+1);
}否则{
总和+=值*深度;
}
});
回报金额;
};

log(depthSum(nestedList))
所以有一种解决方法

从您的
forEach
内部返回没有任何意义,您应该做的是将递归调用中的总计添加到当前总计中。既然这样做了,就不需要将
sum
作为
depthSum
函数的参数

var nestedList=[[1,1],2[1,1]];
var depthSum=函数(嵌套列表,深度=1){
var总和=0;
nestedList.forEach((val)=>{
if(数组isArray(val)){
总和+=深度总和(值,深度+1);
}否则{
总和+=值*深度;
}
});
回报金额;
};

console.log(depthSum(nestedList))
根据Leetcode中代码的要求,以下是工作代码

    var depthSum = function (nestedList,  depth=1) {
    var res = 0;
    nestedList.forEach((val) => {
        if (val.isInteger() === false) {
            res += depthSum(val.getList(),  depth + 1);
        } else {
            res += val.getInteger() * depth;
        }
    });
    return res;
};
您不能使用
Array.isArray()
,因为所有成员都将返回false。而且,您也不能直接访问值或列表。您需要通过他们的API进行访问。函数的输入不仅仅是一个数组。请参见规范中的输入类型和API,如下所示:

     * 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() {
 *         ...
 *     };
 *
 *     Set this NestedInteger to hold a single integer equal to value.
 *     @return {void}
 *     this.setInteger = function(value) {
 *         ...
 *     };
 *
 *     Set this NestedInteger to hold a nested list and adds a nested integer elem to it.
 *     @return {void}
 *     this.add = function(elem) {
 *         ...
 *     };
 *
 *     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() {
 *         ...
 *     };
 * };
 */
/**
 * @param {NestedInteger[]} nestedList
 * @return {number}
 */

根据Leetcode中代码的要求,以下为工作代码

    var depthSum = function (nestedList,  depth=1) {
    var res = 0;
    nestedList.forEach((val) => {
        if (val.isInteger() === false) {
            res += depthSum(val.getList(),  depth + 1);
        } else {
            res += val.getInteger() * depth;
        }
    });
    return res;
};
您不能使用
Array.isArray()
,因为所有成员都将返回false。而且,您也不能直接访问值或列表。您需要通过他们的API进行访问。函数的输入不仅仅是一个数组。请参见规范中的输入类型和API,如下所示:

     * 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() {
 *         ...
 *     };
 *
 *     Set this NestedInteger to hold a single integer equal to value.
 *     @return {void}
 *     this.setInteger = function(value) {
 *         ...
 *     };
 *
 *     Set this NestedInteger to hold a nested list and adds a nested integer elem to it.
 *     @return {void}
 *     this.add = function(elem) {
 *         ...
 *     };
 *
 *     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() {
 *         ...
 *     };
 * };
 */
/**
 * @param {NestedInteger[]} nestedList
 * @return {number}
 */

.forEach()
迭代中
返回
内容没有任何好处。也不要像那样增加
深度
;只要在递归调用中传递
depth+1
,就可以了。谢谢但是当我在leetcode上提交它时,我仍然得到了预期的解决方案,如
NaN
这是我的代码:``var-depthSum=function(nestedList,sum=0,depth=1){for(让I=0;iYou仍然在
.forEach()中得到了无意义的
return
回调。在注释中添加代码效果不太好;如果要添加更多上下文,请更新问题。从
.forEach()返回
内容没有任何好处
iteration。也不要像那样递增
depth
;只需在递归调用中传递
depth+1
。明白了。谢谢!但是当我在leetcode上提交它时,我仍然得到了预期的解决方案,如
NaN
这是我的代码:``var depthSum=function(nestedList,sum=0,depth=1){(设i=0;i您仍然在
.forEach()
回调中得到了无意义的
返回值。在注释中添加代码效果不太好;如果要添加更多上下文,请更新问题。