mirror of
https://gitee.com/coder-xiaomo/leetcode-problemset
synced 2025-01-10 18:48:13 +08:00
66 lines
6.8 KiB
JSON
66 lines
6.8 KiB
JSON
{
|
|
"data": {
|
|
"question": {
|
|
"questionId": "2761",
|
|
"questionFrontendId": "2626",
|
|
"boundTopicId": null,
|
|
"title": "Array Reduce Transformation",
|
|
"titleSlug": "array-reduce-transformation",
|
|
"content": "<p>Given an integer array <code>nums</code>, a reducer function <code>fn</code>, and an initial value <code>init</code>, return a <strong>reduced</strong> array.</p>\n\n<p>A <strong>reduced</strong> array is created by applying the following operation: <code>val = fn(init, nums[0])</code>, <code>val = fn(val, nums[1])</code>, <code>val = fn(val, nums[2])</code>, <code>...</code> until every element in the array has been processed. The final value of <code>val</code> is returned.</p>\n\n<p>If the length of the array is 0, it should return <code>init</code>.</p>\n\n<p>Please solve it without using the built-in <code>Array.reduce</code> method.</p>\n\n<p> </p>\n<p><strong class=\"example\">Example 1:</strong></p>\n\n<pre>\n<strong>Input:</strong> \nnums = [1,2,3,4]\nfn = function sum(accum, curr) { return accum + curr; }\ninit = 0\n<strong>Output:</strong> 10\n<strong>Explanation:</strong>\ninitially, the value is init=0.\n(0) + nums[0] = 1\n(1) + nums[1] = 3\n(3) + nums[2] = 6\n(6) + nums[3] = 10\nThe final answer is 10.\n</pre>\n\n<p><strong class=\"example\">Example 2:</strong></p>\n\n<pre>\n<strong>Input:</strong> \nnums = [1,2,3,4]\nfn = function sum(accum, curr) { return accum + curr * curr; }\ninit = 100\n<strong>Output:</strong> 130\n<strong>Explanation:</strong>\ninitially, the value is init=100.\n(100) + nums[0]^2 = 101\n(101) + nums[1]^2 = 105\n(105) + nums[2]^2 = 114\n(114) + nums[3]^2 = 130\nThe final answer is 130.\n</pre>\n\n<p><strong class=\"example\">Example 3:</strong></p>\n\n<pre>\n<strong>Input:</strong> \nnums = []\nfn = function sum(accum, curr) { return 0; }\ninit = 25\n<strong>Output:</strong> 25\n<strong>Explanation:</strong> For empty arrays, the answer is always init.\n</pre>\n\n<p> </p>\n<p><strong>Constraints:</strong></p>\n\n<ul>\n\t<li><code>0 <= nums.length <= 1000</code></li>\n\t<li><code>0 <= nums[i] <= 1000</code></li>\n\t<li><code>0 <= init <= 1000</code></li>\n</ul>\n",
|
|
"translatedTitle": null,
|
|
"translatedContent": null,
|
|
"isPaidOnly": false,
|
|
"difficulty": "Easy",
|
|
"likes": 473,
|
|
"dislikes": 24,
|
|
"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\": \"Apply Transform Over Each Element in Array\", \"titleSlug\": \"apply-transform-over-each-element-in-array\", \"difficulty\": \"Easy\", \"translatedTitle\": null}]",
|
|
"exampleTestcases": "[1,2,3,4]\nfunction sum(accum, curr) { return accum + curr; }\n0\n[1,2,3,4]\nfunction sum(accum, curr) { return accum + curr * curr; }\n100\n[]\nfunction sum(accum, curr) { return 0; }\n25",
|
|
"categoryTitle": "JavaScript",
|
|
"contributors": [],
|
|
"topicTags": [],
|
|
"companyTagStats": null,
|
|
"codeSnippets": [
|
|
{
|
|
"lang": "JavaScript",
|
|
"langSlug": "javascript",
|
|
"code": "/**\n * @param {number[]} nums\n * @param {Function} fn\n * @param {number} init\n * @return {number}\n */\nvar reduce = function(nums, fn, init) {\n \n};",
|
|
"__typename": "CodeSnippetNode"
|
|
},
|
|
{
|
|
"lang": "TypeScript",
|
|
"langSlug": "typescript",
|
|
"code": "type Fn = (accum: number, curr: number) => number\n\nfunction reduce(nums: number[], fn: Fn, init: number): number {\n \n};",
|
|
"__typename": "CodeSnippetNode"
|
|
}
|
|
],
|
|
"stats": "{\"totalAccepted\": \"81.9K\", \"totalSubmission\": \"98.6K\", \"totalAcceptedRaw\": 81872, \"totalSubmissionRaw\": 98619, \"acRate\": \"83.0%\"}",
|
|
"hints": [
|
|
"Declare a variable \"res\" and set it it equal to the initial value.",
|
|
"Loop over each value in the array and set \"res\" = fn(res, arr[i])."
|
|
],
|
|
"solution": {
|
|
"id": "1889",
|
|
"canSeeDetail": false,
|
|
"paidOnly": true,
|
|
"hasVideoSolution": false,
|
|
"paidOnlyVideo": true,
|
|
"__typename": "ArticleNode"
|
|
},
|
|
"status": null,
|
|
"sampleTestCase": "[1,2,3,4]\nfunction sum(accum, curr) { return accum + curr; }\n0",
|
|
"metaData": "{\n \"name\": \"reduce\",\n \"params\": [\n {\n \"name\": \"nums\",\n \"type\": \"integer[]\"\n },\n {\n \"type\": \"string\",\n \"name\": \"fn\"\n },\n {\n \"type\": \"integer\",\n \"name\": \"init\"\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 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"
|
|
}
|
|
}
|
|
} |