{ "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
.
You may assume the array is the output of JSON.parse
. In other words, it is valid JSON.
Please solve it without using lodash's _.chunk
function.
\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\narr
is a valid JSON array2 <= JSON.stringify(arr).length <= 105
1 <= size <= arr.length + 1
给定一个数组 arr
和一个块大小 size
,返回一个 分块 的数组。分块 的数组包含了 arr
中的原始元素,但是每个子数组的长度都是 size
。如果 arr.length
不能被 size
整除,那么最后一个子数组的长度可能小于 size
。
你可以假设该数组是 JSON.parse
的输出结果。换句话说,它是有效的JSON。
请你在不使用 lodash 的函数 _.chunk
的情况下解决这个问题。
\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解释:\n\nsize
大于arr.length
,因此所有元素都在第一个子数组中。\n
示例 4:
\n\n\n输入:arr = [], size = 1\n输出:[]\n解释:没有元素需要分块,因此返回一个空数组。\n\n
\n\n
提示:
\n\narr
是一个有效的 JSON 数组2 <= JSON.stringify(arr).length <= 105
1 <= size <= arr.length + 1
\\u7248\\u672c\\uff1a \\u60a8\\u7684\\u4ee3\\u7801\\u5728\\u6267\\u884c\\u65f6\\u5c06\\u5e26\\u4e0a 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"
}
}
}Node.js 16.13.2<\\/code><\\/p>\\r\\n\\r\\n
--harmony<\\/code> \\u6807\\u8bb0\\u6765\\u5f00\\u542f \\u65b0\\u7248ES6\\u7279\\u6027<\\/a>\\u3002<\\/p>\\r\\n\\r\\n