• 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
    }
    
  • 相关阅读:
    学习笔记-Python基础19-结构化文件存储-xml
    学习笔记-Python基础18-asyncio异步、aiohttp、concurrent并发
    学习笔记-Python基础17-迭代器、生成器、协成
    学习笔记-Python基础16-多进程
    学习笔记-Python基础16-多线程
    Java中BigDecimal的一个除法异常
    JUnit单元测试入门
    Location search 属性
    struts2表单提交的乱码的问题的解决
    出现java.lang.NoSuchFieldException resourceEntries错误的解决方法
  • 原文地址:https://www.cnblogs.com/mapoos/p/13252888.html
Copyright © 2020-2023  润新知