{ "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.
A 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.
If the length of the array is 0, it should return init
.
Please solve it without using the built-in Array.reduce
method.
\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\n0 <= nums.length <= 1000
0 <= nums[i] <= 1000
0 <= init <= 1000
请你编写一个函数,它的参数为一个整数数组 nums
、一个计算函数 fn
和初始值 init 。返回一个数组 归约后 的值。
你可以定义一个数组 归约后 的值,然后应用以下操作: val = fn(init, nums[0])
, val = fn(val, nums[1])
, val = fn(val, arr[2])
,...
直到数组中的每个元素都被处理完毕。返回 val
的最终值。
如果数组的长度为 0,它应该返回 init
的值。
请你在不使用内置数组方法的 Array.reduce
前提下解决这个问题。
\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\n0 <= nums.length <= 1000
0 <= nums[i] <= 1000
0 <= init <= 1000
\\u7248\\u672c\\uff1a \\u60a8\\u7684\\u4ee3\\u7801\\u5728\\u6267\\u884c\\u65f6\\u5c06\\u5e26\\u4e0a 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"
}
}
}Node.js 16.13.2<\\/code><\\/p>\\r\\n\\r\\n
--harmony<\\/code> \\u6807\\u8bb0\\u6765\\u5f00\\u542f \\u65b0\\u7248ES6\\u7279\\u6027<\\/a>\\u3002<\\/p>\\r\\n\\r\\n