1
0
mirror of https://gitee.com/coder-xiaomo/leetcode-problemset synced 2025-02-04 14:40:27 +08:00
Code Issues Projects Releases Wiki Activity GitHub Gitee
leetcode-problemset/leetcode-cn/problem (Chinese)/分块数组 [chunk-array].html

49 lines
2.0 KiB
HTML
Raw Normal View History

2023-05-15 17:43:00 +08:00
<p>给定一个数组&nbsp;<code>arr</code>&nbsp;和一个块大小&nbsp;<code>size</code>&nbsp;,返回一个 <strong>分块</strong>&nbsp;的数组。<strong>分块</strong>&nbsp;的数组包含了&nbsp;<code>arr</code>&nbsp;中的原始元素,但是每个子数组的长度都是&nbsp;<code>size</code>&nbsp;。如果&nbsp;<code>arr.length</code>&nbsp;不能被&nbsp;<code>size</code>&nbsp;整除,那么最后一个子数组的长度可能小于&nbsp;<code>size</code>&nbsp;</p>
<p>你可以假设该数组是&nbsp;<code>JSON.parse</code>&nbsp;的输出结果。换句话说它是有效的JSON。</p>
<p>请你在不使用 lodash 的函数&nbsp;<code>_.chunk</code>&nbsp;的情况下解决这个问题。</p>
<p>&nbsp;</p>
<p><strong class="example">示例 1</strong></p>
<pre>
<b>输入:</b>arr = [1,2,3,4,5], size = 1
<b>输出:</b>[[1],[2],[3],[4],[5]]
<b>解释:</b>数组 <code>arr </code>被分割成了每个只有一个元素的子数组。
</pre>
<p><strong class="example">示例 2</strong></p>
<pre>
<b>输入:</b>arr = [1,9,6,3,2], size = 3
<b>输出:</b>[[1,9,6],[3,2]]
<b>解释:</b>数组 <code>arr </code>被分割成了每个有三个元素的子数组。然而,第二个子数组只有两个元素。
</pre>
<p><strong class="example">示例 3</strong></p>
<pre>
<b>输入:</b>arr = [8,5,3,2,6], size = 6
<b>输出:</b>[[8,5,3,2,6]]
<b>解释:</b><code>size </code>大于 <code>arr.length </code>,因此所有元素都在第一个子数组中。
</pre>
<p><strong class="example">示例 4</strong></p>
<pre>
<b>输入:</b>arr = [], size = 1
<b>输出:</b>[]
<b>解释:</b>没有元素需要分块,因此返回一个空数组。</pre>
<p>&nbsp;</p>
<p><b>提示:</b></p>
<ul>
<li><code>arr is a valid JSON array</code></li>
<li><code>2 &lt;= JSON.stringify(arr).length &lt;= 10<sup>5</sup></code></li>
<li><code>1 &lt;= size &lt;= arr.length + 1</code></li>
</ul>