1
0
mirror of https://gitee.com/coder-xiaomo/leetcode-problemset synced 2025-01-25 17:50:26 +08:00
Code Issues Projects Releases Wiki Activity GitHub Gitee
leetcode-problemset/leetcode-cn/originData/calculator-with-method-chaining.json

59 lines
13 KiB
JSON
Raw Normal View History

2023-06-12 23:05:37 +08:00
{
"data": {
"question": {
"questionId": "2863",
"questionFrontendId": "2726",
"categoryTitle": "JavaScript",
"boundTopicId": 2300583,
"title": "Calculator with Method Chaining",
"titleSlug": "calculator-with-method-chaining",
2023-12-09 18:42:21 +08:00
"content": "<p>Design a <code>Calculator</code> class. The class should provide the&nbsp;mathematical operations of&nbsp;addition, subtraction, multiplication, division, and exponentiation. It should also allow consecutive operations to be performed using method chaining.&nbsp;The <code>Calculator</code> class constructor should accept a number&nbsp;which serves as the&nbsp;initial value of <code>result</code>.</p>\n\n<p>Your <font face=\"monospace\"><code>Calculator</code>&nbsp;</font>class should have the following methods:</p>\n\n<ul>\n\t<li><code>add</code> - This method adds the given number <code>value</code> to the&nbsp;<code>result</code> and returns the updated <code>Calculator</code>.</li>\n\t<li><code>subtract</code> -&nbsp;This method subtracts the given number <code>value</code>&nbsp;from the&nbsp;<code>result</code> and returns the updated <code>Calculator</code>.</li>\n\t<li><code>multiply</code> -&nbsp;This method multiplies the <code>result</code>&nbsp; by the given number <code>value</code> and returns the updated <code>Calculator</code>.</li>\n\t<li><code>divide</code> -&nbsp;This method divides the <code>result</code> by the given number <code>value</code> and returns the updated <code>Calculator</code>. If the passed value is <code>0</code>, an error <code>&quot;Division by zero is not allowed&quot;</code> should be thrown.</li>\n\t<li><code>power</code> -&nbsp;This method raises the&nbsp;<code>result</code> to the power of the given number <code>value</code> and returns the updated <code>Calculator</code>.</li>\n\t<li><code>getResult</code> -&nbsp;This method returns the <code>result</code>.</li>\n</ul>\n\n<p>Solutions within&nbsp;<code>10<sup>-5</sup></code>&nbsp;of the actual result are considered correct.</p>\n\n<p>&nbsp;</p>\n<p><strong class=\"example\">Example 1:</strong></p>\n\n<pre>\n<strong>Input:</strong> \nactions = [&quot;Calculator&quot;, &quot;add&quot;, &quot;subtract&quot;, &quot;getResult&quot;], \nvalues = [10, 5, 7]\n<strong>Output:</strong> 8\n<strong>Explanation:</strong> \nnew Calculator(10).add(5).subtract(7).getResult() // 10 + 5 - 7 = 8\n</pre>\n\n<p><strong class=\"example\">Example 2:</strong></p>\n\n<pre>\n<strong>Input:</strong> \nactions = [&quot;Calculator&quot;, &quot;multiply&quot;, &quot;power&quot;, &quot;getResult&quot;], \nvalues = [2, 5, 2]\n<strong>Output:</strong> 100\n<strong>Explanation:</strong> \nnew Calculator(2).multiply(5).power(2).getResult() // (2 * 5) ^ 2 = 100\n</pre>\n\n<p><strong class=\"example\">Example 3:</strong></p>\n\n<pre>\n<strong>Input:</strong> \nactions = [&quot;Calculator&quot;, &quot;divide&quot;, &quot;getResult&quot;], \nvalues = [20, 0]\n<strong>Output:</strong> &quot;Division by zero is not allowed&quot;\n<strong>Explanation:</strong> \nnew Calculator(20).divide(0).getResult() // 20 / 0 \n\nThe error should be thrown because we cannot divide by zero.\n</pre>\n\n<p>&nbsp;</p>\n<p><strong>Constraints:</strong></p>\n\n<ul>\n\t<li><code>actions</code> is a valid JSON array of strings</li>\n\t<li><code>values</code>&nbsp;is a valid JSON array of numbers</li>\n\t<li><code>2 &lt;= actions.length &lt;= 2 * 10<sup>4</sup></code></li>\n\t<li><code>1 &lt;= values.length &lt;= 2 * 10<sup>4</sup>&nbsp;- 1</code></li>\n\t<li><code>actions[i]</code> is one of &quot;Calculator&quot;, &quot;add&quot;, &quot;subtract&quot;, &quot;multiply&quot;, &quot;divide&quot;, &quot;power&quot;, and&nbsp;&quot;getResult&quot;</li>\n\t<li>First action is always &quot;Calculator&quot;</li>\n\t<li>Last action is always &quot;getResult&quot;</li>\n</ul>\n",
2023-06-12 23:05:37 +08:00
"translatedTitle": "使用方法链的计算器",
2023-12-09 18:42:21 +08:00
"translatedContent": "<p>设计一个类 <code>Calculator</code> 。该类应提供加法、减法、乘法、除法和乘方等数学运算功能。同时,它还应支持连续操作的方法链式调用。<code>Calculator</code> 类的构造函数应接受一个数字作为 <code>result</code> 的初始值。</p>\n\n<p>你的 <code>Calculator</code> 类应包含以下方法:</p>\n\n<ul>\n\t<li><code>add</code> - 将给定的数字 <code>value</code> 与 <code>result</code> 相加,并返回更新后的 <code>Calculator</code> 对象。</li>\n\t<li><code>subtract</code> - 从 <code>result</code> 中减去给定的数字 <code>value</code>&nbsp;,并返回更新后的 <code>Calculator</code> 对象。</li>\n\t<li><code>multiply</code> - 将 <code>result</code> 乘以给定的数字 <code>value</code> ,并返回更新后的&nbsp;<code>Calculator</code> 对象。</li>\n\t<li><code>divide</code> - 将 <code>result</code> 除以给定的数字 <code>value</code> ,并返回更新后的&nbsp;<code>Calculator</code> 对象。如果传入的值为 <code>0</code> ,则抛出错误 <code>\"Division by zero is not allowed\"</code> 。</li>\n\t<li><code>power</code> - 计算 <code>result</code> 的幂,指数为给定的数字 <code>value</code> ,并返回更新后的&nbsp;<code>Calculator</code> 对象。(<code>result = result ^ value</code> </li>\n\t<li><code>getResult</code> - 返回 <code>result</code> 的值。</li>\n</ul>\n\n<p>结果与实际结果相差在 <code>10<sup>-5</sup></code><sup>&nbsp;</sup>范围内的解被认为是正确的。</p>\n\n<p>&nbsp;</p>\n\n<p><b>示例 1</b></p>\n\n<pre>\n<b>输入:</b>actions = [\"Calculator\", \"add\", \"subtract\", \"getResult\"], \nvalues = [10, 5, 7]\n<b>输出:</b>8\n<b>解释:</b>\nnew Calculator(10).add(5).subtract(7).getResult() // 10 + 5 - 7 = 8\n</pre>\n\n<p><strong class=\"example\">示例 2</strong></p>\n\n<pre>\n<b>输入:</b>actions = [\"Calculator\", \"multiply\", \"power\", \"getResult\"], \nvalues = [2, 5, 2]\n<b>输出:</b>100\n<b>解释:</b>\nnew Calculator(2).multiply(5).power(2).getResult() // (2 * 5) ^ 2 = 100\n</pre>\n\n<p><strong class=\"example\">示例 3</strong></p>\n\n<pre>\n<b>输入:</b>actions = [\"Calculator\", \"divide\", \"getResult\"], \nvalues = [20, 0]\n<b>输出:</b>\"Division by zero is not allowed\"\n<b>解释:</b>\nnew Calculator(20).divide(0).getResult() // 20 / 0 \n\n由于不能除以零因此应抛出错误。\n</pre>\n\n<p>&nbsp;</p>\n\n<p><strong>提示:</strong></p>\n\n<ul>\n\t<li><code>actions</code>&nbsp;是一个有效的 JSON 字符串数组</li>\n\t<li><code>values</code>&nbsp;是一个有效的 JSON 字符串数组</li>\n\t<li><code>2 &lt;= actions.length &lt;= 2 * 10<sup>4</sup></code></li>\n\t<li><code>1 &lt;= values.length &lt;= 2 * 10<sup>4</sup>&nbsp;- 1</code></li>\n\t<li><code>actions[i]</code>&nbsp;是 \"Calculator\", \"add\", \"subtract\", \"multiply\", \"divide\", \"power\", 和 \"getResult\" 其中的元素</li>\n\t<li>第一个操作总是 \"Calculator\"</li>\n\t<li>最后一个操作总是&nbsp;\"getResult\"</li>\n</ul>\n",
2023-06-12 23:05:37 +08:00
"isPaidOnly": false,
"difficulty": "Easy",
2023-12-09 18:42:21 +08:00
"likes": 2,
2023-06-12 23:05:37 +08:00
"dislikes": 0,
"isLiked": null,
"similarQuestions": "[]",
"contributors": [],
2023-12-09 18:42:21 +08:00
"langToValidPlayground": "{\"cpp\": false, \"java\": false, \"python\": true, \"python3\": false, \"mysql\": false, \"mssql\": false, \"oraclesql\": false, \"c\": false, \"csharp\": false, \"javascript\": false, \"typescript\": false, \"bash\": false, \"php\": false, \"swift\": false, \"kotlin\": false, \"dart\": false, \"golang\": false, \"ruby\": false, \"scala\": false, \"html\": false, \"pythonml\": false, \"rust\": false, \"racket\": false, \"erlang\": false, \"elixir\": false, \"pythondata\": false, \"react\": false, \"vanillajs\": false, \"postgresql\": false}",
2023-06-12 23:05:37 +08:00
"topicTags": [],
"companyTagStats": null,
"codeSnippets": [
{
"lang": "JavaScript",
"langSlug": "javascript",
2023-12-09 18:42:21 +08:00
"code": "class Calculator {\n \n /** \n * @param {number} value\n */\n\tconstructor(value) {\n\t\t\n\t}\n\n /** \n * @param {number} value\n * @return {Calculator}\n */\n\tadd(value){\n\t\t\n\t}\n\n /** \n * @param {number} value\n * @return {Calculator}\n */\n\tsubtract(value){\n\t\t\n\t}\n\n /** \n * @param {number} value\n * @return {Calculator}\n */ \n\tmultiply(value) {\n\t\t\n\t}\n\n /** \n * @param {number} value\n * @return {Calculator}\n */\n\tdivide(value) {\n\t\t\n\t}\n \n /** \n * @param {number} value\n * @return {Calculator}\n */\n\tpower(value) {\n\t\t\n\t}\n \n /** \n * @return {number}\n */\n\tgetResult() {\n\t\t\n\t}\n}",
2023-06-12 23:05:37 +08:00
"__typename": "CodeSnippetNode"
},
{
"lang": "TypeScript",
"langSlug": "typescript",
2023-12-09 18:42:21 +08:00
"code": "class Calculator {\n \n\tconstructor(value: number) {\n\t\t\n }\n \n\tadd(value: number): Calculator {\n\t\t\n\t}\n \n\tsubtract(value: number): Calculator {\n\t\t\n\t}\n \n\tmultiply(value: number): Calculator {\n\t\t\n\t}\n\n\tdivide(value: number): Calculator {\n\t\t\n\t}\n \n\tpower(value: number): Calculator {\n\t\t\n\t}\n\n\tgetResult(): number {\n\t\t\n\t}\n}",
2023-06-12 23:05:37 +08:00
"__typename": "CodeSnippetNode"
}
],
2023-12-09 19:57:46 +08:00
"stats": "{\"totalAccepted\": \"1.9K\", \"totalSubmission\": \"3K\", \"totalAcceptedRaw\": 1950, \"totalSubmissionRaw\": 2978, \"acRate\": \"65.5%\"}",
2023-06-12 23:05:37 +08:00
"hints": [],
"solution": null,
"status": null,
"sampleTestCase": "[\"Calculator\", \"add\", \"subtract\", \"getResult\"]\n[10, 5, 7]",
"metaData": "{\n \"name\": \"foobar\",\n \"params\": [\n {\n \"name\": \"actions\",\n \"type\": \"string[]\"\n },\n {\n \"type\": \"double[]\",\n \"name\": \"values\"\n }\n ],\n \"return\": {\n \"type\": \"integer\"\n },\n \"languages\": [\n \"typescript\",\n \"javascript\"\n ],\n \"manual\": true\n}",
"judgerAvailable": true,
"judgeType": "large",
"mysqlSchemas": [],
"enableRunCode": true,
2023-12-09 18:42:21 +08:00
"envInfo": "{\"javascript\":[\"JavaScript\",\"<p>\\u7248\\u672c\\uff1a<code>Node.js 16.13.2<\\/code><\\/p>\\r\\n\\r\\n<p>\\u60a8\\u7684\\u4ee3\\u7801\\u5728\\u6267\\u884c\\u65f6\\u5c06\\u5e26\\u4e0a <code>--harmony<\\/code> \\u6807\\u8bb0\\u6765\\u5f00\\u542f <a href=\\\"http:\\/\\/node.green\\/\\\" target=\\\"_blank\\\">\\u65b0\\u7248ES6\\u7279\\u6027<\\/a>\\u3002<\\/p>\\r\\n\\r\\n<p><a href=\\\"https:\\/\\/lodash.com\\\" target=\\\"_blank\\\">lodash.js<\\/a> \\u5e93\\u5df2\\u7ecf\\u9ed8\\u8ba4\\u88ab\\u5305\\u542b\\u3002<\\/p>\\r\\n\\r\\n<p> \\u5982\\u9700\\u4f7f\\u7528\\u961f\\u5217\\/\\u4f18\\u5148\\u961f\\u5217\\uff0c\\u60a8\\u53ef\\u4f7f\\u7528 <a href=\\\"https:\\/\\/github.com\\/datastructures-js\\/priority-queue\\/tree\\/fb4fdb984834421279aeb081df7af624d17c2a03\\\" target=\\\"_blank\\\"> datastructures-js\\/priority-queue@5.3.0<\\/a> \\u548c <a href=\\\"https:\\/\\/github.com\\/datastructures-js\\/queue\\/tree\\/e63563025a5a805aa16928cb53bcd517bfea9230\\\" target=\\\"_blank\\\"> datastructures-js\\/queue@4.2.1<\\/a>\\u3002<\\/p>\"],\"typescript\":[\"TypeScript\",\"<p>TypeScript 5.1.6<\\/p>\\r\\n\\r\\n<p>Compile Options: --alwaysStrict --strictBindCallApply --strictFunctionTypes --target ES2022<\\/p>\\r\\n\\r\\n<p><a href=\\\"https:\\/\\/lodash.com\\\" target=\\\"_blank\\\">lodash.js<\\/a> \\u5e93\\u5df2\\u7ecf\\u9ed8\\u8ba4\\u88ab\\u5305\\u542b\\u3002<\\/p>\\r\\n\\r\\n<p> \\u5982\\u9700\\u4f7f\\u7528\\u961f\\u5217\\/\\u4f18\\u5148\\u961f\\u5217\\uff0c\\u60a8\\u53ef\\u4f7f\\u7528 <a href=\\\"https:\\/\\/github.com\\/datastructures-js\\/priority-queue\\/tree\\/fb4fdb984834421279aeb081df7af624d17c2a03\\\" target=\\\"_blank\\\"> datastructures-js\\/priority-queue@5.3.0<\\/a> \\u548c <a href=\\\"https:\\/\\/github.com\\/datastructures-js\\/queue\\/tree\\/e63563025a5a805aa16928cb53bcd517bfea9230\\\" target=\\\"_blank\\\"> datastructures-js\\/queue@4.2.1<\\/a>\\u3002<\\/p>\"]}",
2023-06-12 23:05:37 +08:00
"book": null,
"isSubscribed": false,
"isDailyQuestion": false,
"dailyRecordStatus": null,
"editorType": "CKEDITOR",
"ugcQuestionId": null,
"style": "LEETCODE",
"exampleTestcases": "[\"Calculator\", \"add\", \"subtract\", \"getResult\"]\n[10, 5, 7]\n[\"Calculator\", \"multiply\", \"power\", \"getResult\"]\n[2, 5, 2]\n[\"Calculator\", \"divide\", \"getResult\"]\n[20, 0]",
"__typename": "QuestionNode"
}
}
}