Leetcode--30 天 JavaScript 挑战(3)
Skyen Lv4

2629. 复合函数

请你编写一个函数,它接收一个函数数组 [f1, f2, f3,…, fn] ,并返回一个新的函数 fn ,它是函数数组的 复合函数 。

[f(x), g(x), h(x)] 的 复合函数 为 fn(x) = f(g(h(x))) 。

一个空函数列表的 复合函数 是 恒等函数 f(x) = x 。

你可以假设数组中的每个函数接受一个整型参数作为输入,并返回一个整型作为输出。

示例 1:
输入:functions = [x => x + 1, x => x * x, x => 2 * x], x = 4
输出:65
解释:
从右向左计算……
Starting with x = 4.
2 * (4) = 8
(8) * (8) = 64
(64) + 1 = 65

示例 2:
输出:functions = [x => 10 * x, x => 10 * x, x => 10 * x], x = 1
输入:1000
解释:
从右向左计算……
10 * (1) = 10
10 * (10) = 100
10 * (100) = 1000

示例 3:
输入:functions = [], x = 42
输出:42
解释:
空函数列表的复合函数就是恒等函数

提示:
-1000 <= x <= 1000
0 <= functions.length <= 1000
所有函数都接受并返回一个整型

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
/**
* @param {Function[]} functions
* @return {Function}
*/
var compose = function(functions) {
return function(x) {
for(let i = functions.length-1; i >= 0; i--){
x = functions[i](x)
}
return x;
}
};

/**
* const fn = compose([x => x + 1, x => 2 * x])
* fn(4) // 9
*/

2666. 只允许一次函数调用

给定一个函数 fn ,它返回一个新的函数,返回的函数与原始函数完全相同,只不过它确保 fn 最多被调用一次。

第一次调用返回的函数时,它应该返回与 fn 相同的结果。
第一次后的每次调用,它应该返回 undefined 。

示例 1:
输入:fn = (a,b,c) => (a + b + c), calls = [[1,2,3],[2,3,6]]
输出:[{“calls”:1,”value”:6}]
解释:
const onceFn = once(fn);
onceFn(1, 2, 3); // 6
onceFn(2, 3, 6); // undefined, fn 没有被调用

示例 2:
输入:fn = (a,b,c) => (a * b * c), calls = [[5,7,4],[2,3,6],[4,6,8]]
输出:[{“calls”:1,”value”:140}]
解释:
const onceFn = once(fn);
onceFn(5, 7, 4); // 140
onceFn(2, 3, 6); // undefined, fn 没有被调用
onceFn(4, 6, 8); // undefined, fn 没有被调用

提示:
1 <= calls.length <= 10
1 <= calls[i].length <= 100
2 <= JSON.stringify(calls).length <= 1000

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
/**
* @param {Function} fn
* @return {Function}
*/
var once = function(fn) {
let once = null
return function (...args) {
return !once ? once = fn(...args) : undefined
}
};

/**
* let fn = (a,b,c) => (a + b + c)
* let onceFn = once(fn)
*
* onceFn(1,2,3); // 6
* onceFn(2,3,6); // returns undefined without calling fn
*/

2703. 返回传递的参数的长度

请你编写一个函数 argumentsLength,返回传递给该函数的参数数量。

示例 1:
输入:argsArr = [5]
输出:1
解释:
argumentsLength(5); // 1
只传递了一个值给函数,因此它应返回 1。

示例 2:
输入:argsArr = [{}, null, “3”]
输出:3
解释:
argumentsLength({}, null, “3”); // 3
传递了三个值给函数,因此它应返回 3。

提示:
argsArr 是一个有效的 JSON 数组
0 <= argsArr.length <= 100

1
2
3
4
5
6
7
8
9
10
/**
* @return {number}
*/
var argumentsLength = function(...args) {
return args.length;
};

/**
* argumentsLength(1, 2, 3); // 3
*/

2623. 记忆函数

请你编写一个函数,它接收另一个函数作为输入,并返回该函数的 记忆化 后的结果。

记忆函数 是一个对于相同的输入永远不会被调用两次的函数。相反,它将返回一个缓存值。

你可以假设有 3 个可能的输入函数:sum 、fib 和 factorial 。

sum 接收两个整型参数 a 和 b ,并返回 a + b 。
fib 接收一个整型参数 n ,如果 n <= 1 则返回 1,否则返回 fib (n - 1) + fib (n - 2)。
factorial 接收一个整型参数 n ,如果 n <= 1 则返回 1 ,否则返回 factorial(n - 1) * n 。

示例 1:
输入:
“sum”
[“call”,”call”,”getCallCount”,”call”,”getCallCount”]
[[2,2],[2,2],[],[1,2],[]]
输出:
[4,4,1,3,2]
解释:
const sum = (a, b) => a + b;
const memoizedSum = memoize(sum);
memoizedSum (2, 2);// 返回 4。sum() 被调用,因为之前没有使用参数 (2, 2) 调用过。
memoizedSum (2, 2);// 返回 4。没有调用 sum(),因为前面有相同的输入。
//总调用数: 1
memoizedSum(1、2);// 返回 3。sum() 被调用,因为之前没有使用参数 (1, 2) 调用过。
//总调用数: 2

示例 2:
输入:
“factorial”
[“call”,”call”,”call”,”getCallCount”,”call”,”getCallCount”]
[[2],[3],[2],[],[3],[]]
输出:
[2,6,2,2,6,2]
解释:
const factorial = (n) => (n <= 1) ? 1 : (n * factorial(n - 1));
const memoFactorial = memoize(factorial);
memoFactorial(2); // 返回 2。
memoFactorial(3); // 返回 6。
memoFactorial(2); // 返回 2。 没有调用 factorial(),因为前面有相同的输入。
// 总调用数:2
memoFactorial(3); // 返回 6。 没有调用 factorial(),因为前面有相同的输入。
// 总调用数:2

示例 3:
输入:
“fib”
[“call”,”getCallCount”]
[[5],[]]
输出:
[8,1]
解释:
fib(5) = 8
// 总调用数:1

Constraints:
0 <= a, b <= 105
1 <= n <= 10
at most 105 function calls
at most 105 attempts to access callCount
input function is sum, fib, or factorial

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
/**
* @param {Function} fn
*/
function memoize(fn) {
const cache = new Map();
return function(...args) {
const key = JSON.stringify(args)
if (!cache.has(key)) {
cache.set(key, fn(...args));
}
return cache.get(key);
}
}


/**
* let callCount = 0;
* const memoizedFn = memoize(function (a, b) {
* callCount += 1;
* return a + b;
* })
* memoizedFn(2, 3) // 5
* memoizedFn(2, 3) // 5
* console.log(callCount) // 1
*/