mirror of
https://gitee.com/coder-xiaomo/leetcode-problemset
synced 2025-01-11 02:58:13 +08:00
59 lines
2.1 KiB
HTML
59 lines
2.1 KiB
HTML
<p>请你编写一段代码为所有数组实现 <code>snail(rowsCount,colsCount)</code> 方法,该方法将 1D 数组转换为以蜗牛排序的模式的 2D 数组。无效的输入值应该输出一个空数组。当 <code>rowsCount * colsCount !==</code><code>nums.length</code> 时。这个输入被认为是无效的。</p>
|
||
|
||
<p>蜗牛排序从左上角的单元格开始,从当前数组的第一个值开始。然后,它从上到下遍历第一列,接着移动到右边的下一列,并从下到上遍历它。将这种模式持续下去,每列交替变换遍历方向,直到覆盖整个数组。例如,当给定输入数组 <code>[19, 10, 3, 7, 9, 8, 5, 2, 1, 17, 16, 14, 12, 18, 6, 13, 11, 20, 4, 15]</code> ,当 <code>rowsCount = 5</code> 且 <code>colsCount = 4</code> 时,需要输出矩阵如下图所示。注意,矩阵沿箭头方向对应于原数组中数字的顺序</p>
|
||
|
||
<p> </p>
|
||
|
||
<p><img alt="Traversal Diagram" src="https://assets.leetcode.com/uploads/2023/04/10/screen-shot-2023-04-10-at-100006-pm.png" style="width: 275px; height: 343px;" /></p>
|
||
|
||
<p> </p>
|
||
|
||
<p><b>示例 1:</b></p>
|
||
|
||
<pre>
|
||
<b>输入:</b>
|
||
nums = [19, 10, 3, 7, 9, 8, 5, 2, 1, 17, 16, 14, 12, 18, 6, 13, 11, 20, 4, 15]
|
||
rowsCount = 5
|
||
colsCount = 4
|
||
<b>输出:</b>
|
||
[
|
||
[19,17,16,15],
|
||
[10,1,14,4],
|
||
[3,2,12,20],
|
||
[7,5,18,11],
|
||
[9,8,6,13]
|
||
]
|
||
</pre>
|
||
|
||
<p><b>示例 2:</b></p>
|
||
|
||
<pre>
|
||
<b>输入:</b>
|
||
nums = [1,2,3,4]
|
||
rowsCount = 1
|
||
colsCount = 4
|
||
<b>输出:</b>[[1, 2, 3, 4]]
|
||
</pre>
|
||
|
||
<p><b>示例 3:</b></p>
|
||
|
||
<pre>
|
||
<b>输入:</b>
|
||
nums = [1,3]
|
||
rowsCount = 2
|
||
colsCount = 2
|
||
<b>输出:</b>[]
|
||
<strong>Explanation:</strong> 2 * 2 = 4, 且原数组 [1,3] 的长度为 2; 所以,输入是无效的。
|
||
</pre>
|
||
|
||
<p> </p>
|
||
|
||
<p><b>提示:</b></p>
|
||
|
||
<ul>
|
||
<li><code>0 <= nums.length <= 250</code></li>
|
||
<li><code>1 <= nums[i] <= 1000</code></li>
|
||
<li><code>1 <= rowsCount <= 250</code></li>
|
||
<li><code>1 <= colsCount <= 250</code></li>
|
||
</ul>
|