mirror of
https://gitee.com/coder-xiaomo/leetcode-problemset
synced 2025-01-10 18:48:13 +08:00
43 lines
1.3 KiB
HTML
43 lines
1.3 KiB
HTML
<p>给定一个函数 <code>fn</code> ,它返回一个新的函数,返回的函数与原始函数完全相同,只不过它确保 <code>fn</code> 最多被调用一次。</p>
|
||
|
||
<ul>
|
||
<li>第一次调用返回的函数时,它应该返回与 <code>fn</code> 相同的结果。</li>
|
||
<li>第一次后的每次调用,它应该返回 <code>undefined</code> 。</li>
|
||
</ul>
|
||
|
||
<p> </p>
|
||
|
||
<p><b>示例 1:</b></p>
|
||
|
||
<pre>
|
||
<b>输入:</b>fn = (a,b,c) => (a + b + c), calls = [[1,2,3],[2,3,6]]
|
||
<b>输出:</b>[{"calls":1,"value":6}]
|
||
<strong>解释:</strong>
|
||
const onceFn = once(fn);
|
||
onceFn(1, 2, 3); // 6
|
||
onceFn(2, 3, 6); // undefined, fn 没有被调用
|
||
</pre>
|
||
|
||
<p><strong class="example">示例 2:</strong></p>
|
||
|
||
<pre>
|
||
<b>输入:</b>fn = (a,b,c) => (a * b * c), calls = [[5,7,4],[2,3,6],[4,6,8]]
|
||
<b>输出:</b>[{"calls":1,"value":140}]
|
||
<strong>解释:</strong>
|
||
const onceFn = once(fn);
|
||
onceFn(5, 7, 4); // 140
|
||
onceFn(2, 3, 6); // undefined, fn 没有被调用
|
||
onceFn(4, 6, 8); // undefined, fn 没有被调用
|
||
</pre>
|
||
|
||
<p> </p>
|
||
|
||
<p><strong>提示:</strong></p>
|
||
|
||
<ul>
|
||
<li><code>calls</code> 是一个有效的 JSON 数组</li>
|
||
<li><code>1 <= calls.length <= 10</code></li>
|
||
<li><code>1 <= calls[i].length <= 100</code></li>
|
||
<li><code>2 <= JSON.stringify(calls).length <= 1000</code></li>
|
||
</ul>
|