{ "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 intial value init, return a reduced array.

\n\n

reduced array is created by applying the following operation: val = fn(init, nums[0]), val = fn(val, nums[1])val = fn(val, arr[2])... until every element in the array has been processed. The final value of val is returned.

\n\n

If the length of the array is 0, it 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]^2 = 101\n(101) + nums[1]^2 = 105\n(105) + nums[2]^2 = 114\n(114) + nums[3]^2 = 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 、一个计算函数 fn 和初始值 init 。返回一个数组 归约后 的值。

\n\n

你可以定义一个数组 归约后 的值,然后应用以下操作: val = fn(init, nums[0]) , val = fn(val, nums[1]) , val = fn(val, arr[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=0 。\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": 0, "dislikes": 0, "isLiked": null, "similarQuestions": "[]", "contributors": [], "langToValidPlayground": "{\"cpp\": true, \"java\": true, \"python\": true, \"python3\": true, \"mysql\": false, \"mssql\": false, \"oraclesql\": false, \"c\": false, \"csharp\": false, \"javascript\": false, \"ruby\": false, \"bash\": false, \"swift\": false, \"golang\": false, \"scala\": false, \"html\": false, \"pythonml\": false, \"kotlin\": false, \"rust\": false, \"php\": false, \"typescript\": false, \"racket\": false, \"erlang\": false, \"elixir\": false, \"dart\": 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\": \"290\", \"totalSubmission\": \"355\", \"totalAcceptedRaw\": 290, \"totalSubmissionRaw\": 355, \"acRate\": \"81.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 16.13.2<\\/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.3.0<\\/a> \\u548c datastructures-js\\/queue@4.2.1<\\/a>\\u3002<\\/p>\"],\"typescript\":[\"TypeScript\",\"

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

Compile Options: --alwaysStrict --strictBindCallApply --strictFunctionTypes --target ES2020<\\/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.3.0<\\/a> \\u548c datastructures-js\\/queue@4.2.1<\\/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" } } }