LeetCode 题解工作台

蜗牛排序

请你编写一段代码为所有数组实现 snail(rowsCount,colsCount) 方法,该方法将 1D 数组转换为以蜗牛排序的模式的 2D 数组。无效的输入值应该输出一个空数组。当 rowsCount * colsCount !== nums.length 时。这个输入被认为是无效的。 蜗牛排序…

category

0

题型

code_blocks

1

代码语言

hub

0

相关题

当前训练重点

中等 · Snail Traversal core interview pattern

bolt

答案摘要

我们首先判断数组的长度是否等于行数与列数的乘积,如果不等,说明输入是无效的,返回空数组。 接下来,我们可以模拟蜗牛排序的过程,从左上角开始,遍历数组,按照蜗牛排序的顺序,将遍历到的元素依次放入结果数组中。

Interview AiBox logo

Interview AiBox 实时 AI 助手,陪你讲清 Snail Traversal core interview pattern 题型思路

试试 AiBox 面试助手arrow_forward
description

题目描述

请你编写一段代码为所有数组实现  snail(rowsCount,colsCount) 方法,该方法将 1D 数组转换为以蜗牛排序的模式的 2D 数组。无效的输入值应该输出一个空数组。当 rowsCount * colsCount !==nums.length 时。这个输入被认为是无效的。

蜗牛排序从左上角的单元格开始,从当前数组的第一个值开始。然后,它从上到下遍历第一列,接着移动到右边的下一列,并从下到上遍历它。将这种模式持续下去,每列交替变换遍历方向,直到覆盖整个数组。例如,当给定输入数组  [19, 10, 3, 7, 9, 8, 5, 2, 1, 17, 16, 14, 12, 18, 6, 13, 11, 20, 4, 15] ,当 rowsCount = 5 且 colsCount = 4 时,需要输出矩阵如下图所示。注意,矩阵沿箭头方向对应于原数组中数字的顺序

 

Traversal Diagram

 

示例 1:

输入:
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
输出:
[
 [19,17,16,15],
 [10,1,14,4],
 [3,2,12,20],
 [7,5,18,11],
 [9,8,6,13]
]

示例 2:

输入:
nums = [1,2,3,4]
rowsCount = 1
colsCount = 4
输出:[[1, 2, 3, 4]]

示例 3:

输入:
nums = [1,3]
rowsCount = 2
colsCount = 2
输出:[]
Explanation: 2 * 2 = 4, 且原数组 [1,3] 的长度为 2; 所以,输入是无效的。

 

提示:

  • 0 <= nums.length <= 250
  • 1 <= nums[i] <= 1000
  • 1 <= rowsCount <= 250
  • 1 <= colsCount <= 250
lightbulb

解题思路

方法一:模拟

我们首先判断数组的长度是否等于行数与列数的乘积,如果不等,说明输入是无效的,返回空数组。

接下来,我们可以模拟蜗牛排序的过程,从左上角开始,遍历数组,按照蜗牛排序的顺序,将遍历到的元素依次放入结果数组中。

时间复杂度 (n)(n),其中 nn 为数组的长度。忽略答案数组的空间消耗,空间复杂度 O(1)O(1)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
declare global {
    interface Array<T> {
        snail(rowsCount: number, colsCount: number): number[][];
    }
}

Array.prototype.snail = function (rowsCount: number, colsCount: number): number[][] {
    if (rowsCount * colsCount !== this.length) {
        return [];
    }
    const ans: number[][] = Array.from({ length: rowsCount }, () => Array(colsCount));
    for (let h = 0, i = 0, j = 0, k = 1; h < this.length; ++h) {
        ans[i][j] = this[h];
        i += k;
        if (i === rowsCount || i === -1) {
            i -= k;
            k = -k;
            ++j;
        }
    }
    return ans;
};

/**
 * const arr = [1,2,3,4];
 * arr.snail(1,4); // [[1,2,3,4]]
 */
speed

复杂度分析

指标
时间complexity is O(N) because every element is visited once. Space complexity is O(N) due to the resulting 2D array holding all elements from the original array.
空间O(N)
psychology

面试官常问的追问

外企场景
  • question_mark

    Ask about handling invalid input arrays that do not match rowsCount * colsCount.

  • question_mark

    Check if the candidate correctly alternates column directions for snail traversal.

  • question_mark

    Expect discussion on efficient O(N) column-wise filling without redundant operations.

warning

常见陷阱

外企场景
  • error

    Not validating the input length before constructing the matrix.

  • error

    Filling all columns in the same direction, breaking the snail pattern.

  • error

    Incorrectly computing row indices for bottom-to-top traversal.

swap_horiz

进阶变体

外企场景
  • arrow_right_alt

    Snail traversal row-wise instead of column-wise with alternating directions.

  • arrow_right_alt

    Generating the traversal order as a flat 1D output instead of a 2D matrix.

  • arrow_right_alt

    Handling dynamic resizing or non-rectangular matrices with padding.

help

常见问题

外企场景

蜗牛排序题解:Snail Traversal core in… | LeetCode #2624 中等