0059. Spiral Matrix Ⅱ (M)

Spiral Matrix II (M)

题目

Given a positive integer n, generate a square matrix filled with elements from 1 to \(n^2\) in spiral order.

Example:

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

题意

将数\(1-n^2\)按照螺旋顺时针的顺序填入一个 n x n 的矩阵中。

思路

方法与 0054. Spiral Matrix 一样,在实现细节上甚至更加简单。


代码实现

Java

模拟

class Solution {
    public int[][] generateMatrix(int n) {
        int[][] matrix = new int[n][n];
        int[] iPlus = {0, 1, 0, -1};
        int[] jPlus = {1, 0, -1, 0};
        int direction = 0;				// 0123分别代表右下左上
        int i = 0, j = 0;
        
        for (int num = 1; num <= n * n; num++) {
            matrix[i][j] = num;
            // 先判断以当前方向走到的下一个位置是否合法,不合法则转向
            int nextI = i + iPlus[direction];
            int nextJ = j + jPlus[direction];
            if (nextI == -1 || nextI == n || nextJ == -1 || nextJ == n || matrix[nextI][nextJ] != 0) {
                direction = (direction + 1) % 4;
                i += iPlus[direction];
                j += jPlus[direction];
            } else {
                i = nextI;
                j = nextJ;
            }
        }
        
        return matrix;
    }
}

层遍历

class Solution {
    public int[][] generateMatrix(int n) {
        int[][] matrix = new int[n][n];
        // 四个参数确定四条外边
        int rowUp = 0, rowDown = n - 1;
        int colLeft = 0, colRight = n - 1;
        int num = 1;

        while (rowUp <= rowDown && colLeft <= colRight) {
            for (int c = colLeft; c <= colRight; c++) {
                matrix[rowUp][c] = num++;
            }
            for (int r = rowUp + 1; r <= rowDown; r++) {
                matrix[r][colRight] = num++;
            }
            // 只有当前层不是一直线时,才有下边和左边
            if (rowUp < rowDown && colLeft < colRight) {
                for (int c = colRight - 1; c > colLeft; c--) {
                    matrix[rowDown][c] = num++;
                }
                for (int r = rowDown; r > rowUp; r--) {
                    matrix[r][colLeft] = num++;
                }
            }
            
            // 四边向内推进一层
            rowUp++;
            rowDown--;
            colLeft++;
            colRight--;
        }

        return matrix;
    }
}

JavaScript

模拟

/**
 * @param {number} n
 * @return {number[][]}
 */
var generateMatrix = function (n) {
  let matrix = new Array(n).fill(0).map(v => [])
  let num = 1
  let stepI = [0, 1, 0, -1]
  let stepJ = [1, 0, -1, 0]
  let dir = 0
  let i = 0, j = 0

  while (num <= n * n) {
    matrix[i][j] = num++
    let nextI = i + stepI[dir]
    let nextJ = j + stepJ[dir]
    if (nextI >= n || nextI < 0 || nextJ >= n || nextJ < 0 || matrix[nextI][nextJ]) {
      dir = (dir + 1) % 4
      nextI = i + stepI[dir]
      nextJ = j + stepJ[dir]
    }
    i = nextI
    j = nextJ
  }
  
  return matrix
}

层遍历

/**
 * @param {number} n
 * @return {number[][]}
 */
var generateMatrix = function (n) {
  let matrix = new Array(n).fill(0).map(v => [])
  let num = 1
  let left = 0, right = n - 1, top = 0, bottom = n - 1
  while (left <= right && top <= bottom) {
    for (let i = left; i <= right; i++) {
      matrix[top][i] = num++
    }
    for (let i = top + 1; i <= bottom; i++) {
      matrix[i][right] = num++
    }
    if (left < right && top < bottom) {
      for (let i = right - 1; i >= left; i--) {
        matrix[bottom][i] = num++
      }
      for (let i = bottom - 1; i > top; i--) {
        matrix[i][left] = num++
      }
    }
    left++
    right--
    top++
    bottom--
  }
  return matrix
}
posted @ 2020-07-06 02:49  墨云黑  阅读(137)  评论(0编辑  收藏  举报