{ "data": { "question": { "questionId": "2761", "questionFrontendId": "2626", "categoryTitle": "JavaScript", "boundTopicId": 2222280, "title": "Array Reduce Transformation", "titleSlug": "array-reduce-transformation", "content": "

Given an integer array nums, a reducer function fn, and an initial value init, return the final result obtained by executing the fn function on each element of the array, sequentially, passing in the return value from the calculation on the preceding element.

\n\n

This result is achieved through the following operations: val = fn(init, nums[0]), val = fn(val, nums[1]), val = fn(val, nums[2]), ... until every element in the array has been processed. The ultimate value of val is then returned.

\n\n

If the length of the array is 0, the function should return init.

\n\n

Please solve it without using the built-in Array.reduce method.

\n\n

 

\n

Example 1:

\n\n
\nInput: \nnums = [1,2,3,4]\nfn = function sum(accum, curr) { return accum + curr; }\ninit = 0\nOutput: 10\nExplanation:\ninitially, the value is init=0.\n(0) + nums[0] = 1\n(1) + nums[1] = 3\n(3) + nums[2] = 6\n(6) + nums[3] = 10\nThe final answer is 10.\n
\n\n

Example 2:

\n\n
\nInput: \nnums = [1,2,3,4]\nfn = function sum(accum, curr) { return accum + curr * curr; }\ninit = 100\nOutput: 130\nExplanation:\ninitially, the value is init=100.\n(100) + nums[0] * nums[0] = 101\n(101) + nums[1] * nums[1] = 105\n(105) + nums[2] * nums[2] = 114\n(114) + nums[3] * nums[3] = 130\nThe final answer is 130.\n
\n\n

Example 3:

\n\n
\nInput: \nnums = []\nfn = function sum(accum, curr) { return 0; }\ninit = 25\nOutput: 25\nExplanation: For empty arrays, the answer is always init.\n
\n\n

 

\n

Constraints:

\n\n\n", "translatedTitle": "数组归约运算", "translatedContent": "

给定一个整数数组 nums、一个 reducer 函数 fn 和一个初始值 init,返回通过依次对数组的每个元素执行 fn 函数得到的最终结果。

\n\n

通过以下操作实现这个结果:val = fn(init, nums[0]),val = fn(val, nums[1]),val = fn(val, nums[2]),... 直到处理数组中的每个元素。然后返回 val 的最终值。

\n\n

如果数组的长度为 0,则函数应返回 init

\n\n

请你在不使用内置数组方法的 Array.reduce 前提下解决这个问题。

\n\n

 

\n\n

示例 1:

\n\n
\n输入:\nnums = [1,2,3,4]\nfn = function sum(accum, curr) { return accum + curr; }\ninit = 0\n输出:10\n解释:\n初始值为 init=0 。\n(0) + nums[0] = 1\n(1) + nums[1] = 3\n(3) + nums[2] = 6\n(6) + nums[3] = 10\nVal 最终值为 10。\n
\n\n

示例 2:

\n\n
\n输入: \nnums = [1,2,3,4]\nfn = function sum(accum, curr) { return accum + curr * curr; }\ninit = 100\n输出:130\n解释:\n初始值为 init=100 。\n(100) + nums[0]^2 = 101\n(101) + nums[1]^2 = 105\n(105) + nums[2]^2 = 114\n(114) + nums[3]^2 = 130\nVal 最终值为 130。\n
\n\n

示例3:

\n\n
\n输入: \nnums = []\nfn = function sum(accum, curr) { return 0; }\ninit = 25\n输出:25\n解释:这是一个空数组,所以返回 init 。\n
\n\n

 

\n\n

提示:

\n\n\n", "isPaidOnly": false, "difficulty": "Easy", "likes": 13, "dislikes": 0, "isLiked": null, "similarQuestions": "[{\"title\": \"Group By\", \"titleSlug\": \"group-by\", \"difficulty\": \"Medium\", \"translatedTitle\": \"\\u5206\\u7ec4\", \"isPaidOnly\": false}, {\"title\": \"Filter Elements from Array\", \"titleSlug\": \"filter-elements-from-array\", \"difficulty\": \"Easy\", \"translatedTitle\": \"\\u8fc7\\u6ee4\\u6570\\u7ec4\\u4e2d\\u7684\\u5143\\u7d20\", \"isPaidOnly\": false}, {\"title\": \"Apply Transform Over Each Element in Array\", \"titleSlug\": \"apply-transform-over-each-element-in-array\", \"difficulty\": \"Easy\", \"translatedTitle\": \"\\u8f6c\\u6362\\u6570\\u7ec4\\u4e2d\\u7684\\u6bcf\\u4e2a\\u5143\\u7d20\", \"isPaidOnly\": false}]", "contributors": [], "langToValidPlayground": "{\"cpp\": true, \"java\": true, \"python\": true, \"python3\": true, \"mysql\": false, \"mssql\": false, \"oraclesql\": false, \"c\": false, \"csharp\": false, \"javascript\": false, \"typescript\": false, \"bash\": false, \"php\": false, \"swift\": false, \"kotlin\": false, \"dart\": false, \"golang\": false, \"ruby\": false, \"scala\": false, \"html\": false, \"pythonml\": false, \"rust\": false, \"racket\": false, \"erlang\": false, \"elixir\": false, \"pythondata\": false, \"react\": false, \"vanillajs\": false, \"postgresql\": false, \"cangjie\": false}", "topicTags": [], "companyTagStats": null, "codeSnippets": [ { "lang": "JavaScript", "langSlug": "javascript", "code": "/**\n * @param {number[]} nums\n * @param {Function} fn\n * @param {number} init\n * @return {number}\n */\nvar reduce = function(nums, fn, init) {\n \n};", "__typename": "CodeSnippetNode" }, { "lang": "TypeScript", "langSlug": "typescript", "code": "type Fn = (accum: number, curr: number) => number\n\nfunction reduce(nums: number[], fn: Fn, init: number): number {\n \n};", "__typename": "CodeSnippetNode" } ], "stats": "{\"totalAccepted\": \"14.6K\", \"totalSubmission\": \"18.4K\", \"totalAcceptedRaw\": 14629, \"totalSubmissionRaw\": 18354, \"acRate\": \"79.7%\"}", "hints": [ "Declare a variable \"res\" and set it it equal to the initial value.", "Loop over each value in the array and set \"res\" = fn(res, arr[i])." ], "solution": null, "status": null, "sampleTestCase": "[1,2,3,4]\nfunction sum(accum, curr) { return accum + curr; }\n0", "metaData": "{\n \"name\": \"reduce\",\n \"params\": [\n {\n \"name\": \"nums\",\n \"type\": \"integer[]\"\n },\n {\n \"type\": \"string\",\n \"name\": \"fn\"\n },\n {\n \"type\": \"integer\",\n \"name\": \"init\"\n }\n ],\n \"return\": {\n \"type\": \"integer\"\n },\n \"manual\": true,\n \"languages\": [\n \"javascript\",\n \"typescript\"\n ]\n}", "judgerAvailable": true, "judgeType": "large", "mysqlSchemas": [], "enableRunCode": true, "envInfo": "{\"javascript\":[\"JavaScript\",\"

\\u7248\\u672c\\uff1aNode.js 20.10.0<\\/code><\\/p>\\r\\n\\r\\n

\\u60a8\\u7684\\u4ee3\\u7801\\u5728\\u6267\\u884c\\u65f6\\u5c06\\u5e26\\u4e0a --harmony<\\/code> \\u6807\\u8bb0\\u6765\\u5f00\\u542f \\u65b0\\u7248ES6\\u7279\\u6027<\\/a>\\u3002<\\/p>\\r\\n\\r\\n

lodash.js<\\/a> \\u5e93\\u5df2\\u7ecf\\u9ed8\\u8ba4\\u88ab\\u5305\\u542b\\u3002<\\/p>\\r\\n\\r\\n

\\u5982\\u9700\\u4f7f\\u7528\\u961f\\u5217\\/\\u4f18\\u5148\\u961f\\u5217\\uff0c\\u60a8\\u53ef\\u4f7f\\u7528 datastructures-js\\/priority-queue@5.4.0<\\/a>\\uff0c datastructures-js\\/queue@4.2.3<\\/a> \\u4ee5\\u53ca datastructures-js\\/deque@1.0.4<\\/a>\\u3002<\\/p>\"],\"typescript\":[\"TypeScript\",\"

TypeScript 5.1.6<\\/p>\\r\\n\\r\\n

Compile Options: --alwaysStrict --strictBindCallApply --strictFunctionTypes --target ES2022<\\/p>\\r\\n\\r\\n

lodash.js<\\/a> \\u5e93\\u5df2\\u7ecf\\u9ed8\\u8ba4\\u88ab\\u5305\\u542b\\u3002<\\/p>\\r\\n\\r\\n

\\u5982\\u9700\\u4f7f\\u7528\\u961f\\u5217\\/\\u4f18\\u5148\\u961f\\u5217\\uff0c\\u60a8\\u53ef\\u4f7f\\u7528 datastructures-js\\/priority-queue@5.4.0<\\/a>\\uff0c datastructures-js\\/queue@4.2.3<\\/a> \\u4ee5\\u53ca datastructures-js\\/deque@1.0.4<\\/a>\\u3002<\\/p>\"]}", "book": null, "isSubscribed": false, "isDailyQuestion": false, "dailyRecordStatus": null, "editorType": "CKEDITOR", "ugcQuestionId": null, "style": "LEETCODE", "exampleTestcases": "[1,2,3,4]\nfunction sum(accum, curr) { return accum + curr; }\n0\n[1,2,3,4]\nfunction sum(accum, curr) { return accum + curr * curr; }\n100\n[]\nfunction sum(accum, curr) { return 0; }\n25", "__typename": "QuestionNode" } } }