【刷题记录】34.顺时针打印矩阵

256 阅读1分钟

Offer 驾到,掘友接招!我正在参与2022春招打卡活动,点击查看活动详情

一、题目描述:

题目来源:LeetCode>顺时针打印矩阵

输入一个矩阵,按照从外向里以顺时针的顺序依次打印出每一个数字。

 

示例 1:

输入:matrix = [[1,2,3],[4,5,6],[7,8,9]]

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

示例 2:

输入:matrix = [[1,2,3,4],[5,6,7,8],[9,10,11,12]]

输出:[1,2,3,4,8,12,11,10,9,5,6,7]  

限制:

0 <= matrix.length <= 100

0 <= matrix[i].length <= 100

二、思路分析:

思路一:

  1. 模拟求解
  2. 模拟打印矩阵的路径
  3. 初始位置是矩阵的左上角,初始方向是向右
  4. 当路径超出界限或者进入之前访问过的位置时,顺时针旋转,进入下一个方向
  5. 判断路径是否进入之前访问过的位置需要使用一个与输入矩阵大小相同的辅助矩阵
  6. 其中的每个元素表示该位置是否被访问过。当一个元素被访问时,将辅助矩阵 中的对应位置的元素设为已访问
  7. 由于矩阵中的每个元素都被访问一次,因此路径的长度即为矩阵中的元素数量,当路径的长度达到矩阵中的元素数量时即为完整路径,将该路径返回

思路二:

  1. 定义出二维数组的左右上下四个边界,left、right、top、bottom;
  2. 循环打印:
  3. 沿着top,从左向右打印,top++;
  4. 沿着right,从上向下打印,right--;
  5. 沿着bottom,从右向左打印,bottom++;
  6. 沿着left,从下向上打印,left++;
  7. 在沿着下边界和左边界打印时,要确保left <= right,top <= bottom。
  8. 对于空矩阵直接返回空数组。

三、AC 代码:

思路一:

    class Solution {
        public int[] spiralOrder(int[][] matrix) {
            if (matrix == null || matrix.length == 0 || matrix[0].length == 0) {
                return new int[0];
            }
            int rowsLength = matrix.length;
            int columnsLength = matrix[0].length;
            boolean[][] visited = new boolean[rowsLength][columnsLength];
            int total = rowsLength * columnsLength;
            int[] result = new int[total];
            int rowIndex = 0, columnIndex = 0;
            int[][] directions = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
            int directionValue = 0;
            for (int i = 0; i < total; i++) {
                result[i] = matrix[rowIndex][columnIndex];
                visited[rowIndex][columnIndex] = true;
                int nextRow = rowIndex + directions[directionValue][0], nextColumn = columnIndex + directions[directionValue][1];
                if (nextRow < 0 || nextRow >= rowsLength || nextColumn < 0 || nextColumn >= columnsLength || visited[nextRow][nextColumn]) {
                    directionValue = (directionValue + 1) % 4;
                }
                rowIndex += directions[directionValue][0];
                columnIndex += directions[directionValue][1];
            }
            return result;
        }
    }

思路二:

    class Solution {
        public int[] spiralOrder(int[][] matrix) {
            if (matrix == null || matrix.length == 0) return new int[0];
            int leftValue = 0, top = 0;
            int rightValue = matrix[0].length - 1;
            int bottomValue = matrix.length - 1;
            int[] result = new int[(rightValue + 1) * (bottomValue + 1)];
            int index = 0;
            while (top <= bottomValue && leftValue <= rightValue) {
                for (int i = leftValue; i <= rightValue; i++) {
                    result[index++] = matrix[top][i];
                }
                top++;
                for (int i = top; i <= bottomValue; i++) {
                    result[index++] = matrix[i][rightValue];
                }
                rightValue--;
                for (int i = rightValue; i >= leftValue && top <= bottomValue; i--) {
                    result[index++] = matrix[bottomValue][i];
                }
                bottomValue--;
                for (int i = bottomValue; i >= top && leftValue <= rightValue; i--) {
                    result[index++] = matrix[i][leftValue];
                }
                leftValue++;
            }
            return result;
        }
    }