Skip to content

Commit e0ed751

Browse files
committed
Add solution #2626
1 parent b69f6d9 commit e0ed751

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,7 @@
383383
2621|[Sleep](./2621-sleep.js)|Easy|
384384
2622|[Cache With Time Limit](./2622-cache-with-time-limit.js)|Medium|
385385
2623|[Memoize](./2623-memoize.js)|Medium|
386+
2626|[Array Reduce Transformation](./2626-array-reduce-transformation.js)|Easy|
386387
2627|[Debounce](./2627-debounce.js)|Medium|
387388
2629|[Function Composition](./2629-function-composition.js)|Easy|
388389
2630|[Memoize II](./2630-memoize-ii.js)|Hard|
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**
2+
* 2626. Array Reduce Transformation
3+
* https://leetcode.com/problems/array-reduce-transformation/
4+
* Difficulty: Easy
5+
*
6+
* Given an integer array nums, a reducer function fn, and an initial value init, return the final
7+
* result obtained by executing the fn function on each element of the array, sequentially,
8+
* passing in the return value from the calculation on the preceding element.
9+
*
10+
* This result is achieved through the following operations: val = fn(init, nums[0]),
11+
* val = fn(val, nums[1]), val = fn(val, nums[2]), ... until every element in the array has been
12+
* processed. The ultimate value of val is then returned.
13+
*
14+
* If the length of the array is 0, the function should return init.
15+
*
16+
* Please solve it without using the built-in Array.reduce method.
17+
*/
18+
19+
/**
20+
* @param {number[]} nums
21+
* @param {Function} fn
22+
* @param {number} init
23+
* @return {number}
24+
*/
25+
var reduce = function(nums, fn, init) {
26+
let result = init;
27+
28+
for (let i = 0; i < nums.length; i++) {
29+
result = fn(result, nums[i]);
30+
}
31+
32+
return result;
33+
};

0 commit comments

Comments
 (0)