{ "data": { "question": { "questionId": "2798", "questionFrontendId": "2677", "categoryTitle": "JavaScript", "boundTopicId": 2267105, "title": "Chunk Array", "titleSlug": "chunk-array", "content": "

Given an array arr and a chunk size size, return a chunked array. A chunked array contains the original elements in arr, but consists of subarrays each of length size. The length of the last subarray may be less than size if arr.length is not evenly divisible by size.

\n\n

You may assume the array is the output of JSON.parse. In other words, it is valid JSON.

\n\n

Please solve it without using lodash's _.chunk function.

\n\n

 

\n

Example 1:

\n\n
\nInput: arr = [1,2,3,4,5], size = 1\nOutput: [[1],[2],[3],[4],[5]]\nExplanation: The arr has been split into subarrays each with 1 element.\n
\n\n

Example 2:

\n\n
\nInput: arr = [1,9,6,3,2], size = 3\nOutput: [[1,9,6],[3,2]]\nExplanation: The arr has been split into subarrays with 3 elements. However, only two elements are left for the 2nd subarray.\n
\n\n

Example 3:

\n\n
\nInput: arr = [8,5,3,2,6], size = 6\nOutput: [[8,5,3,2,6]]\nExplanation: Size is greater than arr.length thus all elements are in the first subarray.\n
\n\n

Example 4:

\n\n
\nInput: arr = [], size = 1\nOutput: []\nExplanation: There are no elements to be chunked so an empty array is returned.
\n\n

 

\n

Constraints:

\n\n\n", "translatedTitle": "分块数组", "translatedContent": "

给定一个数组 arr 和一个块大小 size ,返回一个 分块 的数组。分块 的数组包含了 arr 中的原始元素,但是每个子数组的长度都是 size 。如果 arr.length 不能被 size 整除,那么最后一个子数组的长度可能小于 size 。

\n\n

你可以假设该数组是 JSON.parse 的输出结果。换句话说,它是有效的JSON。

\n\n

请你在不使用 lodash 的函数 _.chunk 的情况下解决这个问题。

\n\n

 

\n\n

示例 1:

\n\n
\n输入:arr = [1,2,3,4,5], size = 1\n输出:[[1],[2],[3],[4],[5]]\n解释:数组 arr 被分割成了每个只有一个元素的子数组。\n
\n\n

示例 2:

\n\n
\n输入:arr = [1,9,6,3,2], size = 3\n输出:[[1,9,6],[3,2]]\n解释:数组 arr 被分割成了每个有三个元素的子数组。然而,第二个子数组只有两个元素。\n
\n\n

示例 3:

\n\n
\n输入:arr = [8,5,3,2,6], size = 6\n输出:[[8,5,3,2,6]]\n解释:size 大于 arr.length ,因此所有元素都在第一个子数组中。\n
\n\n

示例 4:

\n\n
\n输入:arr = [], size = 1\n输出:[]\n解释:没有元素需要分块,因此返回一个空数组。
\n\n

 

\n\n

提示:

\n\n\n", "isPaidOnly": false, "difficulty": "Easy", "likes": 5, "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, \"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}", "topicTags": [], "companyTagStats": null, "codeSnippets": [ { "lang": "JavaScript", "langSlug": "javascript", "code": "/**\n * @param {Array} arr\n * @param {number} size\n * @return {Array}\n */\nvar chunk = function(arr, size) {\n \n};", "__typename": "CodeSnippetNode" }, { "lang": "TypeScript", "langSlug": "typescript", "code": "type JSONValue = null | boolean | number | string | JSONValue[] | { [key: string]: JSONValue };\ntype Obj = Record | Array;\n\nfunction chunk(arr: Obj[], size: number): Obj[][] {\n\t\n};", "__typename": "CodeSnippetNode" } ], "stats": "{\"totalAccepted\": \"4.5K\", \"totalSubmission\": \"6.1K\", \"totalAcceptedRaw\": 4503, \"totalSubmissionRaw\": 6145, \"acRate\": \"73.3%\"}", "hints": [], "solution": null, "status": null, "sampleTestCase": "[1,2,3,4,5]\n1", "metaData": "{\n \"name\": \"chunkify\",\n \"params\": [\n {\n \"name\": \"arr\",\n \"type\": \"string\"\n },\n {\n \"type\": \"integer\",\n \"name\": \"size\"\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\",\"

\\u7248\\u672c\\uff1aNode.js 16.13.2<\\/code><\\/p>\\r\\n\\r\\n

\\u60a8\\u7684\\u4ee3\\u7801\\u5728\\u6267\\u884c\\u65f6\\u5c06\\u5e26\\u4e0a --harmony<\\/code> \\u6807\\u8bb0\\u6765\\u5f00\\u542f \\u65b0\\u7248ES6\\u7279\\u6027<\\/a>\\u3002<\\/p>\\r\\n\\r\\n

lodash.js<\\/a> \\u5e93\\u5df2\\u7ecf\\u9ed8\\u8ba4\\u88ab\\u5305\\u542b\\u3002<\\/p>\\r\\n\\r\\n

\\u5982\\u9700\\u4f7f\\u7528\\u961f\\u5217\\/\\u4f18\\u5148\\u961f\\u5217\\uff0c\\u60a8\\u53ef\\u4f7f\\u7528 datastructures-js\\/priority-queue@5.3.0<\\/a> \\u548c datastructures-js\\/queue@4.2.1<\\/a>\\u3002<\\/p>\"],\"typescript\":[\"TypeScript\",\"

TypeScript 5.1.6<\\/p>\\r\\n\\r\\n

Compile Options: --alwaysStrict --strictBindCallApply --strictFunctionTypes --target ES2022<\\/p>\\r\\n\\r\\n

lodash.js<\\/a> \\u5e93\\u5df2\\u7ecf\\u9ed8\\u8ba4\\u88ab\\u5305\\u542b\\u3002<\\/p>\\r\\n\\r\\n

\\u5982\\u9700\\u4f7f\\u7528\\u961f\\u5217\\/\\u4f18\\u5148\\u961f\\u5217\\uff0c\\u60a8\\u53ef\\u4f7f\\u7528 datastructures-js\\/priority-queue@5.3.0<\\/a> \\u548c datastructures-js\\/queue@4.2.1<\\/a>\\u3002<\\/p>\"]}", "book": null, "isSubscribed": false, "isDailyQuestion": false, "dailyRecordStatus": null, "editorType": "CKEDITOR", "ugcQuestionId": null, "style": "LEETCODE", "exampleTestcases": "[1,2,3,4,5]\n1\n[1,9,6,3,2]\n3\n[8,5,3,2,6]\n6\n[]\n1", "__typename": "QuestionNode" } } }