LeetCode 题解工作台

使用方法链的计算器

设计一个类 Calculator 。该类应提供加法、减法、乘法、除法和乘方等数学运算功能。同时,它还应支持连续操作的方法链式调用。 Calculator 类的构造函数应接受一个数字作为 result 的初始值。 你的 Calculator 类应包含以下方法: add - 将给定的数字 value 与…

category

0

题型

code_blocks

1

代码语言

hub

0

相关题

当前训练重点

简单 · calculator·method·chaining·core·interview·pattern

bolt

答案摘要

class Calculator { private x: number;

Interview AiBox logo

Interview AiBox 实时 AI 助手,陪你讲清 calculator·method·chaining·core·interview·pattern 题型思路

试试 AiBox 面试助手arrow_forward
description

题目描述

设计一个类 Calculator 。该类应提供加法、减法、乘法、除法和乘方等数学运算功能。同时,它还应支持连续操作的方法链式调用。Calculator 类的构造函数应接受一个数字作为 result 的初始值。

你的 Calculator 类应包含以下方法:

  • add - 将给定的数字 valueresult 相加,并返回更新后的 Calculator 对象。
  • subtract - 从 result 中减去给定的数字 value ,并返回更新后的 Calculator 对象。
  • multiply - 将 result 乘以给定的数字 value ,并返回更新后的 Calculator 对象。
  • divide - 将 result 除以给定的数字 value ,并返回更新后的 Calculator 对象。如果传入的值为 0 ,则抛出错误 "Division by zero is not allowed"
  • power - 计算 result 的幂,指数为给定的数字 value ,并返回更新后的 Calculator 对象。(result = result ^ value
  • getResult - 返回 result 的值。

结果与实际结果相差在 10-5 范围内的解被认为是正确的。

 

示例 1:

输入:actions = ["Calculator", "add", "subtract", "getResult"], 
values = [10, 5, 7]
输出:8
解释:
new Calculator(10).add(5).subtract(7).getResult() // 10 + 5 - 7 = 8

示例 2:

输入:actions = ["Calculator", "multiply", "power", "getResult"], 
values = [2, 5, 2]
输出:100
解释:
new Calculator(2).multiply(5).power(2).getResult() // (2 * 5) ^ 2 = 100

示例 3:

输入:actions = ["Calculator", "divide", "getResult"], 
values = [20, 0]
输出:"Division by zero is not allowed"
解释:
new Calculator(20).divide(0).getResult() // 20 / 0 

由于不能除以零,因此应抛出错误。

 

提示:

  • actions 是一个有效的 JSON 字符串数组
  • values 是一个有效的 JSON 字符串数组
  • 2 <= actions.length <= 2 * 104
  • 1 <= values.length <= 2 * 104 - 1
  • actions[i] 是 "Calculator", "add", "subtract", "multiply", "divide", "power", 和 "getResult" 其中的元素
  • 第一个操作总是 "Calculator"
  • 最后一个操作总是 "getResult"
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
29
30
31
32
33
34
35
36
37
38
39
40
class Calculator {
    private x: number;

    constructor(value: number) {
        this.x = value;
    }

    add(value: number): Calculator {
        this.x += value;
        return this;
    }

    subtract(value: number): Calculator {
        this.x -= value;
        return this;
    }

    multiply(value: number): Calculator {
        this.x *= value;
        return this;
    }

    divide(value: number): Calculator {
        if (value === 0) {
            throw new Error('Division by zero is not allowed');
        }
        this.x /= value;
        return this;
    }

    power(value: number): Calculator {
        this.x **= value;
        return this;
    }

    getResult(): number {
        return this.x;
    }
}
speed

复杂度分析

指标
时间O(1)
空间O(1)
psychology

面试官常问的追问

外企场景
  • question_mark

    Look for an understanding of how method chaining works in object-oriented design.

  • question_mark

    Check if edge cases, especially division by zero, are handled correctly.

  • question_mark

    Ensure that the candidate writes clean, readable code with efficient handling of result retrieval.

warning

常见陷阱

外企场景
  • error

    Failing to return the Calculator instance in each method, thus breaking the chain.

  • error

    Not handling division by zero, which would lead to runtime errors or incorrect results.

  • error

    Misunderstanding the return behavior of `getResult`, which should return the final result after all operations.

swap_horiz

进阶变体

外企场景
  • arrow_right_alt

    Allowing for more advanced operations like square roots or logarithms.

  • arrow_right_alt

    Adding support for complex numbers.

  • arrow_right_alt

    Optimizing the implementation for large numbers of chained operations.

help

常见问题

外企场景

使用方法链的计算器题解:calculator·method·chain… | LeetCode #2726 简单