2023-04-23 22:41:08 +08:00
{
"data" : {
"question" : {
"questionId" : "2747" ,
"questionFrontendId" : "2635" ,
"categoryTitle" : "JavaScript" ,
"boundTopicId" : 2219915 ,
"title" : "Apply Transform Over Each Element in Array" ,
"titleSlug" : "apply-transform-over-each-element-in-array" ,
2023-12-09 18:42:21 +08:00
"content" : "<p>Given an integer array <code>arr</code> and a mapping function <code>fn</code>, return a new array with a transformation applied to each element.</p>\n\n<p>The returned array should be created such that <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> </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> </p>\n<p><strong>Constraints:</strong></p>\n\n<ul>\n\t<li><code>0 <= arr.length <= 1000</code></li>\n\t<li><code><font face=\"monospace\">-10<sup>9</sup> <= arr[i] <= 10<sup>9</sup></font></code></li>\n\t<li><code>fn</code> returns a number</li>\n</ul>\n" ,
2023-04-23 22:41:08 +08:00
"translatedTitle" : "转换数组中的每个元素" ,
2023-12-09 18:42:21 +08:00
"translatedContent" : "<p>编写一个函数,这个函数接收一个整数数组 <code>arr</code> 和一个映射函数 <code>fn</code> ,通过该映射函数返回一个新的数组。</p>\n\n<p>返回数组的创建语句应为 <code>returnedArray[i] = fn(arr[i], i)</code> 。</p>\n\n<p>请你在不使用内置方法 <code>Array.map</code> 的前提下解决这个问题。</p>\n\n<p> </p>\n\n<p><strong class=\"example\">示例 1:</strong></p>\n\n<pre>\n<strong>输入:</strong>arr = [1,2,3], fn = function plusone(n) { return n + 1; }\n<strong>输出:</strong>[2,3,4]\n<strong>解释: </strong>\nconst newArray = map(arr, plusone); // [2,3,4]\n此映射函数返回值是将数组中每个元素的值加 1。\n</pre>\n\n<p><strong class=\"example\">示例</strong><strong class=\"example\"> 2:</strong></p>\n\n<pre>\n<strong>输入:</strong>arr = [1,2,3], fn = function plusI(n, i) { return n + i; }\n<strong>输出:</strong>[1,3,5]\n<strong>解释:</strong>此映射函数返回值根据输入数组索引增加每个值。\n</pre>\n\n<p><strong class=\"example\">示例 3:</strong></p>\n\n<pre>\n<strong>输入:</strong>arr = [10,20,30], fn = function constant() { return 42; }\n<strong>输出:</strong>[42,42,42]\n<strong>解释:</strong>此映射函数返回值恒为 42。\n</pre>\n\n<p> </p>\n\n<p><strong>提示:</strong></p>\n\n<ul>\n\t<li><code>0 <= arr.length <= 1000</code></li>\n\t<li><code><font face=\"monospace\">-10<sup>9</sup> <= arr[i] <= 10<sup>9</sup></font></code></li>\n\t<li><font face=\"monospace\"><code>fn</code> 返回一个数</font></li>\n</ul>\n<span style=\"display:block\"><span style=\"height:0px\"><span style=\"position:absolute\"> </span></span></span>" ,
2023-04-23 22:41:08 +08:00
"isPaidOnly" : false ,
"difficulty" : "Easy" ,
2023-12-09 18:42:21 +08:00
"likes" : 8 ,
2023-04-23 22:41:08 +08:00
"dislikes" : 0 ,
"isLiked" : null ,
"similarQuestions" : "[]" ,
"contributors" : [ ] ,
2023-12-09 18:42:21 +08:00
"langToValidPlayground" : "{\"cpp\": true, \"java\": true, \"python\": true, \"python3\": true, \"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-04-23 22:41:08 +08:00
"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" ,
2023-12-09 18:42:21 +08:00
"code" : "function map(arr: number[], fn: (n: number, i: number) => number): number[] {\n\t\n};" ,
2023-04-23 22:41:08 +08:00
"__typename" : "CodeSnippetNode"
}
] ,
2023-12-09 19:57:46 +08:00
"stats" : "{\"totalAccepted\": \"9.1K\", \"totalSubmission\": \"11.9K\", \"totalAcceptedRaw\": 9149, \"totalSubmissionRaw\": 11908, \"acRate\": \"76.8%\"}" ,
2023-04-23 22:41:08 +08:00
"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 ,
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-04-23 22:41:08 +08:00
"book" : null ,
"isSubscribed" : false ,
"isDailyQuestion" : false ,
"dailyRecordStatus" : null ,
"editorType" : "CKEDITOR" ,
"ugcQuestionId" : null ,
"style" : "LEETCODE" ,
"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]" ,
"__typename" : "QuestionNode"
}
}
}