简体   繁体   English

扩展使用Javascript array.reduce辅助方法

[英]Expanding use of Javascript array.reduce helper method

Background 背景

I am following a course on Udemy which goes over all of the ES6 features. 我正在学习关于Udemy的课程,该课程涵盖了所有ES6功能。 In one of the lessons the instructor talks about using the reduce helper method to solve the popular balance parenthesis interview question. 在其中一个课程中,讲师谈到使用reduce辅助方法来解决流行的平衡括号面试问题。

I can solve this without the reduce method. 没有reduce方法,我可以解决这个问题。 Although with the reduce method it does get the job done in less code. 虽然使用reduce方法,但它确实可以用更少的代码完成工作。 I have been asked to find the depth of the parenthesis in an interview before and was wondering if this could all be done in the same method using reduce. 我之前被要求在一次采访中找到括号的深度,并且想知道是否可以使用reduce在同一方法中完成所有这些操作。

I do not know why this addition to the question confuses me so much but I would like to learn. 我不知道为什么这个问题的补充让我很困惑,但我想学习。

Problem 问题

I have been trying to figure it out for a while and it might be my lack of understanding how reduce works. 我一直试图弄清楚它有一段时间,可能是我缺乏理解如何减少工作。

Example

This uses reduce to return true of false regarding if the parenthesis or open and closed evenly. 如果括号或均匀打开和关闭,则使用reduce返回false的true。

function balanceParens(string) {
    return !string.split("").reduce((counter, char) => {
        // Handle if parens open and close out of order
        if (counter < 0) { return counter; }
        // Add 1 for each open in order
        if (char === "(") { return ++counter; }
        // subtract 1 for each close in order
        if (char === ")") { return --counter; }
        // handle use case if char is not a paren
        return counter;
    }, 0);
}
console.log(balanceParens("((()))"));

Question

How would I return the max depth of the parenthesis using the reduce helper method. 如何使用reduce helper方法返回括号的最大深度。

You could maintain current depth and max depth while reducing. 您可以在减少时保持当前深度和最大深度。

 function maxDepth(string) { return string.split("").reduce(({current, max}, char) => { // Handle if parens open and close out of order if (current < 0) return {current, max} // Add 1 for each open in order if (char === "(") return { current: current + 1, max: Math.max(max, current + 1)} // subtract 1 for each close in order if (char === ")") return { current: current - 1, max} return {current, max} }, {current: 0, max: 0}).max; } console.log(maxDepth("(((()))(((())))()(((((()))))))")); 

Here is a compact version that returns NaN when the parentheses are not balanced. 这是一个紧凑版本,当括号不平衡时返回NaN It uses nested functions in a functional style: 它使用功能样式的嵌套函数:

 function maxDepth(string) { return ( ([depth, max]) => depth ? NaN : max ) ([...string].reduce(([depth, max], ch) => (newDepth => [newDepth, newDepth < 0 ? NaN : Math.max(max, newDepth)]) (depth + (ch === "(") - (ch === ")")) , [0, 0])); } console.log(maxDepth("(((()))(((())))()(((((()))))))")); 

This should answer it! 这应该回答它!

 function balanceParens(string) { let max = 0; let res = string.split("").reduce((counter, char) => { // Handle if parens open and close out of order if (counter < 0) { return counter; } // Add 1 for each open in order if (char === "(") { if(++counter > max) { max = counter; } return counter; } // subtract 1 for each close in order if (char === ")") { return --counter; } // handle use case if char is not a paren return counter; }, 0); console.log("Max depth was :", max); return !res; } console.log(balanceParens("((()(((())))))((((()))))")); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM