LeetCode 题解工作台

只允许一次函数调用

给定一个函数 fn ,它返回一个新的函数,返回的函数与原始函数完全相同,只不过它确保 fn 最多被调用一次。 第一次调用返回的函数时,它应该返回与 fn 相同的结果。 第一次后的每次调用,它应该返回 undefined 。 示例 1: 输入: fn = (a,b,c) => (a + b + c),…

category

0

题型

code_blocks

2

代码语言

hub

0

相关题

当前训练重点

简单 · Allow One Function Call core interview pattern

bolt

答案摘要

type JSONValue = null | boolean | number | string | JSONValue[] | { [key: string]: JSONValue }; type OnceFn = (...args: JSONValue[]) => JSONValue | undefined;

Interview AiBox logo

Interview AiBox 实时 AI 助手,陪你讲清 Allow One Function Call core interview pattern 题型思路

试试 AiBox 面试助手arrow_forward
description

题目描述

给定一个函数 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 没有被调用

 

提示:

  • calls 是一个有效的 JSON 数组
  • 1 <= calls.length <= 10
  • 1 <= calls[i].length <= 100
  • 2 <= JSON.stringify(calls).length <= 1000
lightbulb

解题思路

方法一

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
/**
 * @param {Function} fn
 * @return {Function}
 */
var once = function (fn) {
    let called = false;
    return function (...args) {
        if (!called) {
            called = true;
            return fn(...args);
        }
    };
};

/**
 * 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
 */
speed

复杂度分析

指标
时间and space complexity depend on the function fn and the number of arguments passed. Overhead is minimal, with a single boolean flag and optional result storage, so operations are effectively O(1) per call.
空间Depends on the final approach
psychology

面试官常问的追问

外企场景
  • question_mark

    Check if the candidate tracks function execution state correctly.

  • question_mark

    Observe if they handle extra calls without throwing errors.

  • question_mark

    Look for proper argument forwarding and result preservation in the wrapper.

warning

常见陷阱

外企场景
  • error

    Failing to store the result of the first call, causing loss of output for debugging.

  • error

    Not handling additional arguments correctly, breaking the function signature.

  • error

    Allowing multiple executions due to missing or misplaced execution flag.

swap_horiz

进阶变体

外企场景
  • arrow_right_alt

    Allow multiple calls but limit total executions to a specified count.

  • arrow_right_alt

    Reset the single-call state after a timeout, allowing one call per interval.

  • arrow_right_alt

    Track the last returned value and always return it instead of undefined for extra calls.

help

常见问题

外企场景

只允许一次函数调用题解:Allow One Function Call… | LeetCode #2666 简单