mirror of
https://gitee.com/coder-xiaomo/leetcode-problemset
synced 2025-01-10 18:48:13 +08:00
66 lines
6.5 KiB
JSON
66 lines
6.5 KiB
JSON
{
|
|
"data": {
|
|
"question": {
|
|
"questionId": "2741",
|
|
"questionFrontendId": "2629",
|
|
"boundTopicId": null,
|
|
"title": "Function Composition",
|
|
"titleSlug": "function-composition",
|
|
"content": "<p>Given an array of functions <code>[f<span style=\"font-size: 10.8333px;\">1</span>, f<sub>2</sub>, f<sub>3</sub>, ..., f<sub>n</sub>]</code>, return a new function <code>fn</code> that is the <strong>function composition</strong> of the array of functions.</p>\n\n<p>The <strong>function composition</strong> of <code>[f(x), g(x), h(x)]</code> is <code>fn(x) = f(g(h(x)))</code>.</p>\n\n<p>The <strong>function composition</strong> of an empty list of functions is the <strong>identity function</strong> <code>f(x) = x</code>.</p>\n\n<p>You may assume each function in the array accepts one integer as input and returns one integer as output.</p>\n\n<p> </p>\n<p><strong class=\"example\">Example 1:</strong></p>\n\n<pre>\n<strong>Input:</strong> functions = [x => x + 1, x => x * x, x => 2 * x], x = 4\n<strong>Output:</strong> 65\n<strong>Explanation:</strong>\nEvaluating from right to left ...\nStarting with x = 4.\n2 * (4) = 8\n(8) * (8) = 64\n(64) + 1 = 65\n</pre>\n\n<p><strong class=\"example\">Example 2:</strong></p>\n\n<pre>\n<strong>Input:</strong> functions = [x => 10 * x, x => 10 * x, x => 10 * x], x = 1\n<strong>Output:</strong> 1000\n<strong>Explanation:</strong>\nEvaluating from right to left ...\n10 * (1) = 10\n10 * (10) = 100\n10 * (100) = 1000\n</pre>\n\n<p><strong class=\"example\">Example 3:</strong></p>\n\n<pre>\n<strong>Input:</strong> functions = [], x = 42\n<strong>Output:</strong> 42\n<strong>Explanation:</strong>\nThe composition of zero functions is the identity function</pre>\n\n<p> </p>\n<p><strong>Constraints:</strong></p>\n\n<ul>\n\t<li><code><font face=\"monospace\">-1000 <= x <= 1000</font></code></li>\n\t<li><code><font face=\"monospace\">0 <= functions.length <= 1000</font></code></li>\n\t<li>all functions accept and return a single integer</li>\n</ul>\n",
|
|
"translatedTitle": null,
|
|
"translatedContent": null,
|
|
"isPaidOnly": false,
|
|
"difficulty": "Easy",
|
|
"likes": 524,
|
|
"dislikes": 36,
|
|
"isLiked": null,
|
|
"similarQuestions": "[{\"title\": \"Memoize\", \"titleSlug\": \"memoize\", \"difficulty\": \"Medium\", \"translatedTitle\": null}, {\"title\": \"Counter\", \"titleSlug\": \"counter\", \"difficulty\": \"Easy\", \"translatedTitle\": null}]",
|
|
"exampleTestcases": "[x => x + 1, x => x * x, x => 2 * x]\n4\n[x => 10 * x, x => 10 * x, x => 10 * x]\n1\n[]\n42",
|
|
"categoryTitle": "JavaScript",
|
|
"contributors": [],
|
|
"topicTags": [],
|
|
"companyTagStats": null,
|
|
"codeSnippets": [
|
|
{
|
|
"lang": "JavaScript",
|
|
"langSlug": "javascript",
|
|
"code": "/**\n * @param {Function[]} functions\n * @return {Function}\n */\nvar compose = function(functions) {\n \n\treturn function(x) {\n \n }\n};\n\n/**\n * const fn = compose([x => x + 1, x => 2 * x])\n * fn(4) // 9\n */",
|
|
"__typename": "CodeSnippetNode"
|
|
},
|
|
{
|
|
"lang": "TypeScript",
|
|
"langSlug": "typescript",
|
|
"code": "type F = (x: number) => number;\n\nfunction compose(functions: F[]): F {\n \n\treturn function(x) {\n \n }\n};\n\n/**\n * const fn = compose([x => x + 1, x => 2 * x])\n * fn(4) // 9\n */",
|
|
"__typename": "CodeSnippetNode"
|
|
}
|
|
],
|
|
"stats": "{\"totalAccepted\": \"70.3K\", \"totalSubmission\": \"82.7K\", \"totalAcceptedRaw\": 70350, \"totalSubmissionRaw\": 82748, \"acRate\": \"85.0%\"}",
|
|
"hints": [
|
|
"Start by returning a function that takes in a number and returns a number.",
|
|
"Call each of the functions in the correct order. Each time passing the output of the previous function into the next function."
|
|
],
|
|
"solution": {
|
|
"id": "1891",
|
|
"canSeeDetail": false,
|
|
"paidOnly": true,
|
|
"hasVideoSolution": false,
|
|
"paidOnlyVideo": true,
|
|
"__typename": "ArticleNode"
|
|
},
|
|
"status": null,
|
|
"sampleTestCase": "[x => x + 1, x => x * x, x => 2 * x]\n4",
|
|
"metaData": "{\n \"name\": \"compose\",\n \"params\": [\n {\n \"name\": \"functions\",\n \"type\": \"string\"\n },\n {\n \"type\": \"integer\",\n \"name\": \"x\"\n }\n ],\n \"return\": {\n \"type\": \"string\"\n },\n \"languages\": [\n \"javascript\",\n \"typescript\"\n ],\n \"manual\": true\n}",
|
|
"judgerAvailable": true,
|
|
"judgeType": "large",
|
|
"mysqlSchemas": [],
|
|
"enableRunCode": true,
|
|
"enableTestMode": false,
|
|
"enableDebugger": false,
|
|
"envInfo": "{\"javascript\": [\"JavaScript\", \"<p><code>Node.js 16.13.2</code>.</p>\\r\\n\\r\\n<p>Your code is run with <code>--harmony</code> flag, enabling <a href=\\\"http://node.green/\\\" target=\\\"_blank\\\">new ES6 features</a>.</p>\\r\\n\\r\\n<p><a href=\\\"https://lodash.com\\\" target=\\\"_blank\\\">lodash.js</a> library is included by default.</p>\\r\\n\\r\\n<p>For Priority Queue / Queue data structures, you may use 5.3.0 version of <a href=\\\"https://github.com/datastructures-js/priority-queue/tree/fb4fdb984834421279aeb081df7af624d17c2a03\\\" target=\\\"_blank\\\">datastructures-js/priority-queue</a> and 4.2.1 version of <a href=\\\"https://github.com/datastructures-js/queue/tree/e63563025a5a805aa16928cb53bcd517bfea9230\\\" target=\\\"_blank\\\">datastructures-js/queue</a>.</p>\"], \"typescript\": [\"Typescript\", \"<p><code>TypeScript 5.1.6, Node.js 16.13.2</code>.</p>\\r\\n\\r\\n<p>Your code is run with <code>--harmony</code> flag, enabling <a href=\\\"http://node.green/\\\" target=\\\"_blank\\\">new ES2022 features</a>.</p>\\r\\n\\r\\n<p><a href=\\\"https://lodash.com\\\" target=\\\"_blank\\\">lodash.js</a> library is included by default.</p>\"]}",
|
|
"libraryUrl": null,
|
|
"adminUrl": null,
|
|
"challengeQuestion": null,
|
|
"__typename": "QuestionNode"
|
|
}
|
|
}
|
|
} |