LeetCode—— 498, 对角线遍历

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/lhc2207221755/article/details/88669632
/**
     * 给定一个含有 M x N 个元素的矩阵(M 行,N 列),请以对角线遍历的顺序返回这个矩阵中的所有元素,对角线遍历如下图所示。
     * <p>
     * 输入:
     * [
     * [ 1(0,0), 2(0,1), 3(0,2) ],
     * [ 4(1,0), 5(1,1), 6(1,2) ],
     * [ 7(2,0), 8(2,1), 9(2,2) ]
     * ]
     * <p>
     * 输出:  [1,2,4,7,5,3,6,8,9]
     *
     * @param matrix
     * @return
     */
    public int[] findDiagonalOrder(int[][] matrix) {
        if (matrix.length == 0) {
            return new int[0];
        }
        int row = matrix.length;
        int col = matrix[0].length;
        int totalNum = row * col;
        if (totalNum == 1) {
            return new int[]{matrix[0][0]};
        }
        int result[] = new int[totalNum];
        int currentRow = 0;
        int currentCol = 0;
        int currentIndex = 0; //标记结果数组中当前放置数据的位置
        //奇数坐标向左下遍历,偶数坐标向右上遍历
        while (currentCol < col && currentRow < row) {
            result[currentIndex] = matrix[currentRow][currentCol];
            if ((currentCol + currentRow) % 2 == 0) {
                //偶数
                if (currentCol == col - 1) {
                    currentRow++;
                } else if (currentRow - 1 < 0) {
                    currentCol++;
                } else {
                    currentRow--;
                    currentCol++;
                }

            } else {
                //奇数坐标
                if (currentRow == row - 1) {
                    currentCol++;
                } else if (currentCol - 1 < 0) {
                    currentRow++;
                } else {
                    currentRow++;
                    currentCol--;
                }
            }
            currentIndex++;
        }
        return result;
    }

    @Test
    public void findDiagonalOrderTest() {
        int[][] matrix = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
        System.out.println(JSONObject.toJSONString(findDiagonalOrder(matrix)));
    }

猜你喜欢

转载自blog.csdn.net/lhc2207221755/article/details/88669632