54. Spiral Matrix

Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral order.

For example,
Given the following matrix:

[
[ 1, 2, 3 ],
[ 4, 5, 6 ],
[ 7, 8, 9 ]
]
You should return [1,2,3,6,9,8,7,4,5].
题意就是给定一个二维数组,输出从外向里走过的路径。
自己的做法是定义一个方向数组,从向右开始遍历;用一个二维数组记录已经走过的点;当下一个点到了边界或者是已经走过的点就改变方向;循环退出条件是走过了所有数组中的点。

public List spiralOrder(int[][] matrix) {
    List res = new ArrayList<>();
    if (matrix == null || matrix.length == 0 || matrix[0].length == 0) {
        return res;
    }

    int row = matrix.length;
    int col = matrix[0].length;
    int directionIndex = 0;
    String[] directions = {"right", "down", "left", "up"};
    int[] pos = {0, 0};
    int cnt = 0;
    int total = row * col;
    while (cnt < total) {
        res.add(matrix[pos[0]][pos[1]]);
        matrix[pos[0]][pos[1]] = Integer.MAX_VALUE;
        String direction = directions[directionIndex % directions.length];
        switch (direction) {
            case "right":
                if (pos[1] < col - 1 && matrix[pos[0]][pos[1] + 1] != Integer.MAX_VALUE) {
                    pos[1]++;
                } else {
                    directionIndex++;
                    pos[0]++;
                }
                break;
            case "down":
                if (pos[0] < row - 1 && matrix[pos[0] + 1][pos[1]] != Integer.MAX_VALUE) {
                    pos[0]++;
                } else {
                    directionIndex++;
                    pos[1]--;
                }
                break;
            case "left":
                if (pos[1] > 0 && matrix[pos[0]][pos[1] - 1] != Integer.MAX_VALUE) {
                    pos[1]--;
                } else {
                    directionIndex++;
                    pos[0]--;
                }
                break;
            case "up":
                if (pos[0] > 0 && matrix[pos[0] - 1][pos[1]] != Integer.MAX_VALUE) {
                    pos[0]--;
                } else {
                    directionIndex++;
                    pos[1]++;
                }
                break;
        }
        cnt++;
    }

    return res;
}

discuss中有一种解法非常好,它把这道题看成是在一个围栏里面不断向里走,每次走完一行或者一列,这个围栏就会向内缩小。

public List spiralOrder(int[][] matrix) {
    List res = new ArrayList();
    if (matrix.length == 0) {
        return res;
    }
    
    int rowBegin = 0;
    int rowEnd = matrix.length-1;
    int colBegin = 0;
    int colEnd = matrix[0].length - 1;
    
    while (rowBegin <= rowEnd && colBegin <= colEnd) {
        // Traverse Right
        for (int j = colBegin; j <= colEnd; j ++) {
            res.add(matrix[rowBegin][j]);
        }
        rowBegin++;
        
        // Traverse Down
        for (int j = rowBegin; j <= rowEnd; j ++) {
            res.add(matrix[j][colEnd]);
        }
        colEnd--;
        
        if (rowBegin <= rowEnd) {
            // Traverse Left
            for (int j = colEnd; j >= colBegin; j --) {
                res.add(matrix[rowEnd][j]);
            }
        }
        rowEnd--;
        
        if (colBegin <= colEnd) {
            // Traver Up
            for (int j = rowEnd; j >= rowBegin; j --) {
                res.add(matrix[j][colBegin]);
            }
        }
        colBegin ++;
    }
    
    return res;
}

你可能感兴趣的:(54. Spiral Matrix)