LeetCode 题解工作台

创建 Hello World 函数

请你编写一个名为 createHelloWorld 的函数。它应该返回一个新的函数,该函数总是返回 "Hello World" 。 示例 1: 输入: args = [] 输出: "Hello World" 解释: const f = createHelloWorld(); f(); // "Hel…

category

0

题型

code_blocks

1

代码语言

hub

0

相关题

当前训练重点

简单 · Create Hello World Function core interview pattern

bolt

答案摘要

function createHelloWorld() { return function (...args): string {

Interview AiBox logo

Interview AiBox 实时 AI 助手,陪你讲清 Create Hello World Function core interview pattern 题型思路

试试 AiBox 面试助手arrow_forward
description

题目描述

请你编写一个名为 createHelloWorld 的函数。它应该返回一个新的函数,该函数总是返回 "Hello World" 。

 

示例 1:

输入:args = []
输出:"Hello World"
解释:
const f = createHelloWorld();
f(); // "Hello World"

createHelloWorld 返回的函数应始终返回 "Hello World"。

示例 2:

输入:args = [{},null,42]
输出:"Hello World"
解释:
const f = createHelloWorld();
f({}, null, 42); // "Hello World"

可以传递任何参数给函数,但它应始终返回 "Hello World"。

 

提示:

  • 0 <= args.length <= 10
lightbulb

解题思路

方法一

1
2
3
4
5
6
7
8
9
10
11
function createHelloWorld() {
    return function (...args): string {
        return 'Hello World';
    };
}

/**
 * const f = createHelloWorld();
 * f(); // "Hello World"
 */
speed

复杂度分析

指标
时间Depends on the final approach
空间Depends on the final approach
psychology

面试官常问的追问

外企场景
  • question_mark

    Look for the candidate's understanding of function creation and returning another function.

  • question_mark

    Check if they grasp how to ignore parameters and return a fixed value.

  • question_mark

    Assess if they handle edge cases where arguments might be passed to the returned function.

warning

常见陷阱

外企场景
  • error

    Overcomplicating the function logic or introducing unnecessary complexity.

  • error

    Not returning the correct string or forgetting to return 'Hello World'.

  • error

    Misunderstanding the problem and adding functionality that affects the returned value.

swap_horiz

进阶变体

外企场景
  • arrow_right_alt

    Create a function that returns 'Hello Universe' instead of 'Hello World'.

  • arrow_right_alt

    Modify the function to return a different string based on some internal logic while still ignoring parameters.

  • arrow_right_alt

    Extend the function to handle and ignore different types of arguments (e.g., strings, objects, numbers).

help

常见问题

外企场景

创建 Hello World 函数题解:Create Hello World Func… | LeetCode #2667 简单