2726. 使用方法链的计算器【简单】
备注:题目有些长,但逻辑很简单。
1. 📝 Description
leetcode
设计一个类 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:
输入:
js
actions = ["Calculator", "add", "subtract", "getResult"],
values = [10, 5, 7]
1
2
2
输出:8
解释:new Calculator(10).add(5).subtract(7).getResult() // 10 + 5 - 7 = 8
示例 2:
输入:
js
actions = ["Calculator", "multiply", "power", "getResult"],
values = [2, 5, 2]
1
2
2
输出:100
解释:new Calculator(2).multiply(5).power(2).getResult() // (2 * 5) ^ 2 = 100
示例 3:
输入:
js
actions = ["Calculator", "divide", "getResult"],
values = [20, 0]
1
2
2
输出:"Division by zero is not allowed"
解释:new Calculator(20).divide(0).getResult() // 20 / 0
由于不能除以零,因此应抛出错误。
提示:
actions
是一个有效的 JSON 字符串数组values
是一个有效的 JSON 字符串数组2 <= actions.length <= 2 * 10^4
1 <= values.length <= 2 * 10^4 - 1
actions[i]
是 "Calculator", "add", "subtract", "multiply", "divide", "power", 和 "getResult" 其中的元素- 第一个操作总是 "Calculator"
- 最后一个操作总是 "getResult"
2. 💻 题解.1
javascript
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 new Error('Division by zero is not allowed')
this.val /= value
return this
}
/**
* @param {number} value
* @return {Calculator}
*/
power(value) {
this.val **= value
return this
}
/**
* @return {number}
*/
getResult() {
return this.val
}
}
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
题解分析
- 链式调用,每次
return this
即可。 - 除法运算时校验 value 不能为 0,否则抛出错误
throw new Error('Division by zero is not allowed')