mirror of
https://gitee.com/coder-xiaomo/leetcode-problemset
synced 2025-01-10 18:48:13 +08:00
59 lines
9.7 KiB
JSON
59 lines
9.7 KiB
JSON
{
|
||
"data": {
|
||
"question": {
|
||
"questionId": "2821",
|
||
"questionFrontendId": "2715",
|
||
"categoryTitle": "JavaScript",
|
||
"boundTopicId": 2293968,
|
||
"title": "Execute Cancellable Function With Delay",
|
||
"titleSlug": "execute-cancellable-function-with-delay",
|
||
"content": "<p>Given a function <code>fn</code>, an array or arguments <code>args</code>, and a timeout <code>t</code> in milliseconds, return a cancel function <code>cancelFn</code>.</p>\n\n<p>After a delay of <code>t</code>, <code>fn</code> should be called with <code>args</code> passed as parameters <strong>unless</strong> <code>cancelFn</code> was called first. In that case, <code>fn</code> should never be called.</p>\n\n<p> </p>\n<p><strong class=\"example\">Example 1:</strong></p>\n\n<pre>\n<strong>Input:</strong> fn = (x) => x * 5, args = [2], t = 20, cancelTime = 50\n<strong>Output:</strong> [{"time": 20, "returned": 10}]\n<strong>Explanation:</strong> \nconst cancel = cancellable(fn, [2], 20); // fn(2) called at t=20ms\nsetTimeout(cancel, 50);\n\nthe cancelTime (50ms) is after the delay time (20ms), so fn(2) should be called at t=20ms. The value returned from fn is 10.\n</pre>\n\n<p><strong class=\"example\">Example 2:</strong></p>\n\n<pre>\n<strong>Input:</strong> fn = (x) => x**2, args = [2], t = 100, cancelTime = 50\n<strong>Output:</strong> []\n<strong>Explanation:</strong> fn(2) was never called because cancelTime (50ms) is before the delay time (100ms).\n</pre>\n\n<p><strong class=\"example\">Example 3:</strong></p>\n\n<pre>\n<strong>Input:</strong> fn = (x1, x2) => x1 * x2, args = [2,4], t = 30, cancelTime = 100\n<strong>Output:</strong> [{"time": 30, "returned": 8}]\n<strong>Explanation:</strong> fn(2, 4) was called at t=30ms because cancelTime > t.\n</pre>\n\n<p> </p>\n<p><strong>Constraints:</strong></p>\n\n<ul>\n\t<li><code>fn is a function</code></li>\n\t<li><code>args is a valid JSON array</code></li>\n\t<li><code>1 <= args.length <= 10</code></li>\n\t<li><code><font face=\"monospace\">20 <= t <= 1000</font></code></li>\n\t<li><code><font face=\"monospace\">10 <= cancelT <= 1000</font></code></li>\n</ul>\n",
|
||
"translatedTitle": "执行可取消的延迟函数",
|
||
"translatedContent": "<p>现给定一个函数 <code>fn</code> ,一个参数数组 <code>args</code> 和一个以毫秒为单位的超时时间 <code>t</code> ,返回一个取消函数 <code>cancelFn</code> 。</p>\n\n<p>在经过 <code>t</code> 毫秒的延迟后,<strong>除非</strong> 先调用 <code>cancelFn</code> ,否则 <code>fn</code> 应该以 <code>args</code> 作为参数被调用。并且在这种情况下,<code>fn</code> 不应该被调用。</p>\n\n<p> </p>\n\n<p><strong class=\"example\">示例 1:</strong></p>\n\n<pre>\n<b>输入:</b>fn = (x) => x * 5, args = [2], t = 20, cancelTime = 50\n<b>输出:</b>[{\"time\": 20, \"returned\": 10}]\n<b>解释:</b>\nconst cancel = cancellable(fn, [2], 20); // // 在 t=20ms 时调用 fn(2)\nsetTimeout(cancel, 50);\n\ncancelTime(50ms)在延迟时间(20ms)之后,所以 fn(2) 应该在 t=20ms 时调用。fn 的返回值是 10。\n</pre>\n\n<p><strong class=\"example\">示例 2:</strong></p>\n\n<pre>\n<b>输入:</b>fn = (x) => x**2, args = [2], t = 100, cancelTime = 50\n<b>输出:</b>[]\n<b>解释:</b>fn(2) 从未被调用,因为 cancelTime(50ms)在延迟时间(100ms)之前。\n</pre>\n\n<p><strong class=\"example\">示例 3:</strong></p>\n\n<pre>\n<b>输入:</b>fn = (x1, x2) => x1 * x2, args = [2,4], t = 30, cancelTime = 100\n<b>输出:</b>[{\"time\": 30, \"returned\": 8}]\n<b>解释:</b>fn(2) 从未被调用,因为 cancelTime(50ms)在延迟时间(100ms)之前。\n</pre>\n\n<p> </p>\n\n<p><strong>提示:</strong></p>\n\n<ul>\n\t<li><code>fn 是一个函数</code></li>\n\t<li><code>args 是一个有效的 JSON 数组</code></li>\n\t<li><code>1 <= args.length <= 10</code></li>\n\t<li><code><font face=\"monospace\">20 <= t <= 1000</font></code></li>\n\t<li><code><font face=\"monospace\">10 <= cancelT <= 1000</font></code></li>\n</ul>\n",
|
||
"isPaidOnly": false,
|
||
"difficulty": "Easy",
|
||
"likes": 0,
|
||
"dislikes": 0,
|
||
"isLiked": null,
|
||
"similarQuestions": "[]",
|
||
"contributors": [],
|
||
"langToValidPlayground": "{\"cpp\": true, \"java\": true, \"python\": true, \"python3\": true, \"mysql\": false, \"mssql\": false, \"oraclesql\": false, \"c\": false, \"csharp\": false, \"javascript\": false, \"ruby\": false, \"bash\": false, \"swift\": false, \"golang\": false, \"scala\": false, \"html\": false, \"pythonml\": false, \"kotlin\": false, \"rust\": false, \"php\": false, \"typescript\": false, \"racket\": false, \"erlang\": false, \"elixir\": false, \"dart\": false}",
|
||
"topicTags": [],
|
||
"companyTagStats": null,
|
||
"codeSnippets": [
|
||
{
|
||
"lang": "JavaScript",
|
||
"langSlug": "javascript",
|
||
"code": "/**\n * @param {Function} fn\n * @param {Array} args\n * @param {number} t\n * @return {Function}\n */\nvar cancellable = function(fn, args, t) {\n \n};\n\n/**\n * const result = []\n *\n * const fn = (x) => x * 5\n * const args = [2], t = 20, cancelT = 50\n *\n * const log = (...argsArr) => {\n * result.push(fn(...argsArr))\n * }\n * \n * const cancel = cancellable(fn, args, t);\n * \n * setTimeout(() => {\n * cancel()\n * console.log(result) // [{\"time\":20,\"returned\":10}]\n * }, cancelT)\n */",
|
||
"__typename": "CodeSnippetNode"
|
||
},
|
||
{
|
||
"lang": "TypeScript",
|
||
"langSlug": "typescript",
|
||
"code": "function cancellable(fn: Function, args: any[], t: number): Function {\n\n};\n\n/**\n * const result = []\n *\n * const fn = (x) => x * 5\n * const args = [2], t = 20, cancelT = 50\n *\n * const log = (...argsArr) => {\n * result.push(fn(...argsArr))\n * }\n * \n * const cancel = cancellable(fn, args, t);\n * \n * setTimeout(() => {\n * cancel()\n * console.log(result) // [{\"time\":20,\"returned\":10}]\n * }, cancelT)\n */",
|
||
"__typename": "CodeSnippetNode"
|
||
}
|
||
],
|
||
"stats": "{\"totalAccepted\": \"183\", \"totalSubmission\": \"216\", \"totalAcceptedRaw\": 183, \"totalSubmissionRaw\": 216, \"acRate\": \"84.7%\"}",
|
||
"hints": [],
|
||
"solution": null,
|
||
"status": null,
|
||
"sampleTestCase": "(x) => x * 5\n[2]\n20\n50",
|
||
"metaData": "{\n \"name\": \"cancellable\",\n \"params\": [\n {\n \"name\": \"fn\",\n \"type\": \"string\"\n },\n {\n \"type\": \"string\",\n \"name\": \"args\"\n },\n {\n \"type\": \"integer\",\n \"name\": \"t\"\n },\n {\n \"type\": \"integer\",\n \"name\": \"cancelT\"\n }\n ],\n \"return\": {\n \"type\": \"integer\"\n },\n \"languages\": [\n \"javascript\",\n \"typescript\"\n ],\n \"manual\": true\n}",
|
||
"judgerAvailable": true,
|
||
"judgeType": "large",
|
||
"mysqlSchemas": [],
|
||
"enableRunCode": true,
|
||
"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 4.5.4<\\/p>\\r\\n\\r\\n<p>Compile Options: --alwaysStrict --strictBindCallApply --strictFunctionTypes --target ES2020<\\/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>\"]}",
|
||
"book": null,
|
||
"isSubscribed": false,
|
||
"isDailyQuestion": false,
|
||
"dailyRecordStatus": null,
|
||
"editorType": "CKEDITOR",
|
||
"ugcQuestionId": null,
|
||
"style": "LEETCODE",
|
||
"exampleTestcases": "(x) => x * 5\n[2]\n20\n50\n(x) => x**2\n[2]\n100\n50\n(x1, x2) => x1 * x2\n[2,4]\n20\n100",
|
||
"__typename": "QuestionNode"
|
||
}
|
||
}
|
||
} |