Skip to content

2624. Snail Traversal

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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[][] = [];
  for (let i = 0; i < rowsCount; ++i) {
    ans.push([]);
  }
  for (let i = 0; i < rowsCount; ++i)
    for (let j = 0; j < colsCount; ++j) {
      const k = j * rowsCount + (j % 2 == 0 ? i : rowsCount - 1 - i);
      ans[i][j] = this[k];
    }
  return ans;
};