LeetCode 题解工作台

计数器 II

请你写一个函数 createCounter 。这个函数接收一个初始的整数值 init 。并返回一个包含三个函数的对象。 这三个函数是: increment() 将当前值加 1 并返回。 decrement() 将当前值减 1 并返回。 reset() 将当前值设置为 init 并返回。 示例 1: …

category

0

题型

code_blocks

1

代码语言

hub

0

相关题

当前训练重点

简单 · Counter II core interview pattern

bolt

答案摘要

type ReturnObj = { increment: () => number;

Interview AiBox logo

Interview AiBox 实时 AI 助手,陪你讲清 Counter II core interview pattern 题型思路

试试 AiBox 面试助手arrow_forward
description

题目描述

请你写一个函数 createCounter。这个函数接收一个初始的整数值 init。并返回一个包含三个函数的对象。

这三个函数是:

  • increment() 将当前值加 1 并返回。
  • decrement() 将当前值减 1 并返回。
  • reset() 将当前值设置为 init 并返回。

 

示例 1:

输入:init = 5, calls = ["increment","reset","decrement"]
输出:[6,5,4]
解释:
const counter = createCounter(5);
counter.increment(); // 6
counter.reset(); // 5
counter.decrement(); // 4

示例 2:

输入:init = 0, calls = ["increment","increment","decrement","reset","reset"]
输出:[1,2,1,0,0]
解释:
const counter = createCounter(0);
counter.increment(); // 1
counter.increment(); // 2
counter.decrement(); // 1
counter.reset(); // 0
counter.reset(); // 0

 

提示:

  • -1000 <= init <= 1000
  • 0 <= calls.length <= 1000
  • calls[i] 是 “increment”、“decrement” 和 “reset” 中的一个
lightbulb

解题思路

方法一

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
26
27
28
type ReturnObj = {
    increment: () => number;
    decrement: () => number;
    reset: () => number;
};

function createCounter(init: number): ReturnObj {
    let val = init;
    return {
        increment() {
            return ++val;
        },
        decrement() {
            return --val;
        },
        reset() {
            return (val = init);
        },
    };
}

/**
 * const counter = createCounter(5)
 * counter.increment(); // 6
 * counter.reset(); // 5
 * counter.decrement(); // 4
 */
speed

复杂度分析

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

面试官常问的追问

外企场景
  • question_mark

    Focus on correctly implementing the counter functions and their interactions with the initial state.

  • question_mark

    Ensure that each function operates independently and maintains the expected results.

  • question_mark

    Look for clear, concise, and efficient code that directly addresses the problem requirements.

warning

常见陷阱

外企场景
  • error

    Forgetting to preserve the initial state of the counter, leading to incorrect results when `reset` is called.

  • error

    Not handling edge cases, such as calling `reset` multiple times or performing `increment` and `decrement` in rapid succession.

  • error

    Overcomplicating the solution by introducing unnecessary variables or steps.

swap_horiz

进阶变体

外企场景
  • arrow_right_alt

    Modifying the counter object to support additional operations like `multiply` or `divide`.

  • arrow_right_alt

    Making the counter operations support negative increments or decrements.

  • arrow_right_alt

    Adding support for non-integer initial values (e.g., floating point numbers).

help

常见问题

外企场景

计数器 II题解:Counter II core intervi… | LeetCode #2665 简单