LeetCode 题解工作台
使用方法链的计算器
设计一个类 Calculator 。该类应提供加法、减法、乘法、除法和乘方等数学运算功能。同时,它还应支持连续操作的方法链式调用。 Calculator 类的构造函数应接受一个数字作为 result 的初始值。 你的 Calculator 类应包含以下方法: add - 将给定的数字 value 与…
0
题型
1
代码语言
0
相关题
当前训练重点
简单 · calculator·method·chaining·core·interview·pattern
答案摘要
class Calculator { private x: number;
Interview AiBoxInterview AiBox 实时 AI 助手,陪你讲清 calculator·method·chaining·core·interview·pattern 题型思路
题目描述
设计一个类 Calculator 。该类应提供加法、减法、乘法、除法和乘方等数学运算功能。同时,它还应支持连续操作的方法链式调用。Calculator 类的构造函数应接受一个数字作为 result 的初始值。
你的 Calculator 类应包含以下方法:
add- 将给定的数字value与result相加,并返回更新后的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 * 1041 <= values.length <= 2 * 104 - 1actions[i]是 "Calculator", "add", "subtract", "multiply", "divide", "power", 和 "getResult" 其中的元素- 第一个操作总是 "Calculator"
- 最后一个操作总是 "getResult"
解题思路
方法一
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;
}
}
复杂度分析
| 指标 | 值 |
|---|---|
| 时间 | O(1) |
| 空间 | O(1) |
面试官常问的追问
外企场景- 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.
常见陷阱
外企场景- 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.
进阶变体
外企场景- 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.