跳至主要內容

2726. 使用方法链的计算器


2726. 使用方法链的计算器open in new window

🟢   🔗 力扣open in new window LeetCodeopen in new window

题目

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 number value to the result and returns the updated Calculator.
  • subtract: This method subtracts the given number value from the result and returns the updated Calculator.
  • multiply: This method multiplies the result by the given number value and returns the updated Calculator.
  • divide: This method divides the result by the given number value and returns the updated Calculator. If the passed value is 0, an error "Division by zero is not allowed" should be thrown.
  • power: This method raises the result to the power of the given number value and returns the updated Calculator.
  • getResult: This method returns the result.

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 strings
  • values is a valid JSON array of numbers
  • 2 <= 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: 将给定的数字 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 范围内的解被认为是正确的。

提示:

  • 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"

解题思路

  1. 定义计算器类:创建一个类 Calculator,包含一个内部状态属性用于存储当前的计算值。
  2. 实现方法:实现 add, subtract, multiply, 和 divide 方法,每个方法都接受一个数字,并更新当前值。
  3. 返回当前对象:每个方法都应返回 this,以便支持链式调用。
  4. 实现 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;
	}
}