2726. 使用方法链的计算器
2726. 使用方法链的计算器
题目
Design a Calculator
class. The class should provide the mathematical operations of addition, subtraction, multiplication, division, and exponentiation. It should also allow consecutive operations to be performed using method chaining. The Calculator
class constructor should accept a number which serves as the initial value of result
.
Your Calculator
class should have the following methods:
add
: This method adds the given numbervalue
to theresult
and returns the updatedCalculator
.subtract
: This method subtracts the given numbervalue
from theresult
and returns the updatedCalculator
.multiply
: This method multiplies theresult
by the given numbervalue
and returns the updatedCalculator
.divide
: This method divides theresult
by the given numbervalue
and returns the updatedCalculator
. If the passed value is0
, an error"Division by zero is not allowed"
should be thrown.power
: This method raises theresult
to the power of the given numbervalue
and returns the updatedCalculator
.getResult
: This method returns theresult
.
Solutions within 10-5
of the actual result are considered correct.
Example 1:
Input:
actions = ["Calculator", "add", "subtract", "getResult"],
values = [10, 5, 7]
Output: 8
Explanation:
new Calculator(10).add(5).subtract(7).getResult() // 10 + 5 - 7 = 8
Example 2:
Input:
actions = ["Calculator", "multiply", "power", "getResult"],
values = [2, 5, 2]
Output: 100
Explanation:
new Calculator(2).multiply(5).power(2).getResult() // (2 * 5) ^ 2 = 100
Example 3:
Input:
actions = ["Calculator", "divide", "getResult"],
values = [20, 0]
Output: "Division by zero is not allowed"
Explanation:
new Calculator(20).divide(0).getResult() // 20 / 0
The error should be thrown because we cannot divide by zero.
Constraints:
actions
is a valid JSON array of stringsvalues
is a valid JSON array of numbers2 <= actions.length <= 2 * 10^4
1 <= values.length <= 2 * 104 - 1
actions[i]
is one of "Calculator", "add", "subtract", "multiply", "divide", "power", and "getResult"- First action is always "Calculator"
- Last action is always "getResult"
题目大意
设计一个类 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
范围内的解被认为是正确的。
提示:
actions
是一个有效的 JSON 字符串数组values
是一个有效的 JSON 字符串数组2 <= actions.length <= 2 * 10^4
1 <= values.length <= 2 * 104 - 1
actions[i]
是 "Calculator", "add", "subtract", "multiply", "divide", "power", 和 "getResult" 其中的元素- 第一个操作总是 "Calculator"
- 最后一个操作总是 "getResult"
解题思路
- 定义计算器类:创建一个类
Calculator
,包含一个内部状态属性用于存储当前的计算值。 - 实现方法:实现
add
,subtract
,multiply
, 和divide
方法,每个方法都接受一个数字,并更新当前值。 - 返回当前对象:每个方法都应返回
this
,以便支持链式调用。 - 实现
getResult
方法:该方法返回当前的计算结果。
复杂度分析
- 时间复杂度:
O(1)
,每个操作都是常数时间。 - 空间复杂度:
O(1)
,只使用了固定的额外空间。
代码
class Calculator {
/**
* @param {number} value
*/
constructor(value) {
this.val = value; // 初始化当前值
}
/**
* @param {number} value
* @return {Calculator}
*/
add(value) {
this.val += value;
return this; // 返回当前对象
}
/**
* @param {number} value
* @return {Calculator}
*/
subtract(value) {
this.val -= value;
return this; // 返回当前对象
}
/**
* @param {number} value
* @return {Calculator}
*/
multiply(value) {
this.val *= value;
return this; // 返回当前对象
}
/**
* @param {number} value
* @return {Calculator}
*/
divide(value) {
// 抛出异常
if (value == 0) throw 'Division by zero is not allowed';
this.val /= value;
return this; // 返回当前对象
}
/**
* @param {number} value
* @return {Calculator}
*/
power(value) {
this.val = Math.pow(this.val, value);
return this; // 返回当前对象
}
/**
* @return {number}
*/
getResult() {
return this.val;
}
}