js版 Leetcode spiral-matrix-ii

问题

原题地址: https://leetcode.com/problems/spiral-matrix-ii/

Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order.

For example, Given n = 3,

You should return the following matrix:

[
 [ 1, 2, 3 ],
 [ 8, 9, 4 ],
 [ 7, 6, 5 ]
]

JS 版 148ms 通过结果

/**
 * @param {number} n
 * @returns {number[][]}
 */
var generateMatrix = function (n) {
  var result = [];
  var index = 0;
  for (; index < n; index++) {
    result.push(new Array(n));
  }
  var i = 0,
    j = 0,
    directionX = 0,
    directionY = 1;
  index = 1;
  while (index <= Math.pow(n, 2)) {
    result[i][j] = index++;
    if (directionX === 0 && directionY === 1 && (j === n - 1 || result[i][j + 1] !== undefined)) {
      directionX = 1;
      directionY = 0;
    } else if (directionX === 1 && directionY === 0 && (i === n - 1 || result[i + 1][j] !== undefined)) {
      directionX = 0;
      directionY = -1;
    } else if (directionX === 0 && directionY === -1 && (j === 0 || result[i][j - 1] !== undefined)) {
      directionX = -1;
      directionY = 0;
    } else if (directionX === -1 && directionY === 0 && (i === 0 || result[i - 1][j] !== undefined)) {
      directionX = 0;
      directionY = 1;
    }
    i += directionX;
    j += directionY;
  }
  return result;
};
The End