高级农民
- 积分
- 4142
- 大米
- 颗
- 鳄梨
- 个
- 水井
- 尺
- 蓝莓
- 颗
- 萝卜
- 根
- 小米
- 粒
- 学分
- 个
- 注册时间
- 2017-1-22
- 最后登录
- 1970-1-1
|
2624. Snail Traversal
Medium
Hint
Write code that enhances all arrays such that you can call the snail(rowsCount, colsCount) method that transforms the 1D array into a 2D array organised in the pattern known as snail traversal order. Invalid input values should output an empty array. If rowsCount * colsCount !== nums.length, the input is considered invalid.
Snail traversal order starts at the top left cell with the first value of the current array. It then moves through the entire first column from top to bottom, followed by moving to the next column on the right and traversing it from bottom to top. This pattern continues, alternating the direction of traversal with each column, until the entire current array is covered. For example, when given the input array [19, 10, 3, 7, 9, 8, 5, 2, 1, 17, 16, 14, 12, 18, 6, 13, 11, 20, 4, 15] with rowsCount = 5 and colsCount = 4, the desired output matrix is shown below. Note that iterating the matrix following the arrows corresponds to the order of numbers in the original array.
Traversal Diagram
Example 1:
Input:
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
Output:
[
[19,17,16,15],
[10,1,14,4],
[3,2,12,20],
[7,5,18,11],
[9,8,6,13]
]
Example 2:
Input:
nums = [1,2,3,4]
rowsCount = 1
colsCount = 4
Output: [[1, 2, 3, 4]]
Example 3:
Input:
nums = [1,3]
rowsCount = 2
colsCount = 2
Output: []
Explanation: 2 multiplied by 2 is 4, and the original array [1,3] has a length of 2; therefore, the input is invalid.
Constraints:
0 <= nums.length <= 250
1 <= nums[i] <= 1000
1 <= rowsCount <= 250
1 <= colsCount <= 250
解题思路与关键点说明边界处理:如果数组长度不等于 rowsCount * colsCount,输入不合法,直接返回 []。二维数组初始化:使用 Array.from({ length: rowsCount }, () => []) 创建一个拥有 rowsCount 行的空二维数组。蛇形填充逻辑:原数组元素按照列优先顺序填充:第 0 列:占用原数组索引 $0 \sim (\text{rowsCount} - 1)$第 1 列:占用原数组索引 $\text{rowsCount} \sim (2 \times \text{rowsCount} - 1)$,以此类推。列索引计算:col = Math.floor(i / rowsCount)行索引计算:根据列索引的奇偶性决定填充方向:偶数列 (col % 2 === 0):自顶向下遍历,行索引为 i % rowsCount。奇数列 (col % 2 === 1):自底向上遍历,行索引为 rowsCount - 1 - (i % rowsCount)- interface Array<T> {
- snail(rowsCount: number, colsCount: number): number[][];
- }
- Array.prototype.snail = function(rowsCount: number, colsCount: number): number[][] {
- // 1. 校验输入合法性:元素总数必须等于 rowsCount * colsCount
- if (this.length !== rowsCount * colsCount) {
- return [];
- }
- // 2. 初始化 rowsCount 行的二维数组
- const result: number[][] = Array.from({ length: rowsCount }, () => []);
- // 3. 遍历原数组的每个元素,计算对应的行号和列号
- for (let i = 0; i < this.length; i++) {
- // 计算当前元素属于第几列
- const col = Math.floor(i / rowsCount);
-
- // 计算行号:
- // 偶数列(0, 2, 4...)从上到下填充:行号为 i % rowsCount
- // 奇数列(1, 3, 5...)从下到上填充:行号为 rowsCount - 1 - (i % rowsCount)
- const row = (col % 2 === 0)
- ? (i % rowsCount)
- : (rowsCount - 1 - (i % rowsCount));
- result[row][col] = this[i];
- }
- return result;
- }
- /**
- * const arr = [1,2,3,4];
- * arr.snail(1,4); // [[1,2,3,4]]
- */
复制代码 |
|