1
0
mirror of https://gitee.com/coder-xiaomo/leetcode-problemset synced 2025-01-10 18:48:13 +08:00
Code Issues Projects Releases Wiki Activity GitHub Gitee
leetcode-problemset/leetcode/originData/apply-transform-over-each-element-in-array.json
2023-04-14 14:39:57 +08:00

59 lines
5.9 KiB
JSON

{
"data": {
"question": {
"questionId": "2747",
"questionFrontendId": "2635",
"boundTopicId": null,
"title": "Apply Transform Over Each Element in Array",
"titleSlug": "apply-transform-over-each-element-in-array",
"content": "<p>Given an integer array&nbsp;<code>arr</code>&nbsp;and a mapping function&nbsp;<code>fn</code>, return&nbsp;a new array with a transformation applied to each element.</p>\n\n<p>The returned array should be created such that&nbsp;<code>returnedArray[i] = fn(arr[i], i)</code>.</p>\n\n<p>Please solve it without the built-in <code>Array.map</code> method.</p>\n\n<p>&nbsp;</p>\n<p><strong class=\"example\">Example 1:</strong></p>\n\n<pre>\n<strong>Input:</strong> arr = [1,2,3], fn = function plusone(n) { return n + 1; }\n<strong>Output:</strong> [2,3,4]\n<strong>Explanation:</strong>\nconst newArray = map(arr, plusone); // [2,3,4]\nThe function increases each value in the array by one. \n</pre>\n\n<p><strong class=\"example\">Example 2:</strong></p>\n\n<pre>\n<strong>Input:</strong> arr = [1,2,3], fn = function plusI(n, i) { return n + i; }\n<strong>Output:</strong> [1,3,5]\n<strong>Explanation:</strong> The function increases each value by the index it resides in.\n</pre>\n\n<p><strong class=\"example\">Example 3:</strong></p>\n\n<pre>\n<strong>Input:</strong> arr = [10,20,30], fn = function constant() { return 42; }\n<strong>Output:</strong> [42,42,42]\n<strong>Explanation:</strong> The function always returns 42.\n</pre>\n\n<p>&nbsp;</p>\n<p><strong>Constraints:</strong></p>\n\n<ul>\n\t<li><code>0 &lt;= arr.length &lt;= 1000</code></li>\n\t<li><code><font face=\"monospace\">-10<sup>9</sup>&nbsp;&lt;= arr[i] &lt;= 10<sup>9</sup></font></code></li>\n\t<li><font face=\"monospace\"><code>fn returns a number</code></font></li>\n</ul>\n",
"translatedTitle": null,
"translatedContent": null,
"isPaidOnly": false,
"difficulty": "Easy",
"likes": 27,
"dislikes": 1,
"isLiked": null,
"similarQuestions": "[{\"title\": \"Group By\", \"titleSlug\": \"group-by\", \"difficulty\": \"Medium\", \"translatedTitle\": null}, {\"title\": \"Filter Elements from Array\", \"titleSlug\": \"filter-elements-from-array\", \"difficulty\": \"Easy\", \"translatedTitle\": null}, {\"title\": \"Array Reduce Transformation\", \"titleSlug\": \"array-reduce-transformation\", \"difficulty\": \"Easy\", \"translatedTitle\": null}]",
"exampleTestcases": "function plusone(n) { return n + 1; }\n[1,2,3]\nfunction plusI(n, i) { return n + i; }\n[1,2,3]\nfunction constant(n, i) { return 42; }\n[10,20,30]",
"categoryTitle": "JavaScript",
"contributors": [],
"topicTags": [],
"companyTagStats": null,
"codeSnippets": [
{
"lang": "JavaScript",
"langSlug": "javascript",
"code": "/**\n * @param {number[]} arr\n * @param {Function} fn\n * @return {number[]}\n */\nvar map = function(arr, fn) {\n \n};",
"__typename": "CodeSnippetNode"
},
{
"lang": "TypeScript",
"langSlug": "typescript",
"code": "function map(arr: number[], fn: (n: number, i: number) => number): number[] {\n\n};",
"__typename": "CodeSnippetNode"
}
],
"stats": "{\"totalAccepted\": \"1.2K\", \"totalSubmission\": \"1.3K\", \"totalAcceptedRaw\": 1183, \"totalSubmissionRaw\": 1290, \"acRate\": \"91.7%\"}",
"hints": [
"Start by creating an array that will eventually be returned.",
"Loop over each element in the passed array. Push fn(arr[i]) to the returned array."
],
"solution": null,
"status": null,
"sampleTestCase": "function plusone(n) { return n + 1; }\n[1,2,3]",
"metaData": "{\n \"name\": \"map\",\n \"params\": [\n {\n \"name\": \"arr\",\n \"type\": \"integer[]\"\n },\n {\n \"type\": \"string\",\n \"name\": \"fn\"\n }\n ],\n \"return\": {\n \"type\": \"integer[]\"\n },\n \"manual\": true,\n \"languages\": [\n \"javascript\",\n \"typescript\"\n ]\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 4.5.4, 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 ES2020 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"
}
}
}