题解 | #螺旋矩阵#

螺旋矩阵

http://www.nowcoder.com/practice/7edf70f2d29c4b599693dc3aaeea1d31

import java.util.ArrayList;
public class Solution {
    // 四中状态的
    enum Status {
        LeftToRight, TopToBottom, RightToLeft, BottomToTop
    }

    private int[][] init(int row, int col) {
        int[][] visited = new int[row][col];
        for (int i = 0; i < visited.length; i++) {
            for (int j = 0; j < visited[0].length; j++) {
                visited[i][j] = 0; // unvisited
            }
        }
        return visited;
    }

    private boolean hasUnVisitedElement(int[][] visited) {
        for (int i = 0; i < visited.length; i++) {
            for (int j = 0; j < visited[0].length; j++) {
                if (visited[i][j] == 0) return true;
            }
        }
        return false;
    }

    public ArrayList<Integer> spiralOrder(int[][] matrix) {
        ArrayList<Integer> result = new ArrayList<>();
        if(matrix.length==0) return result;
        else if(matrix.length == 1){
            for (int i = 0; i < matrix[0].length; i++) {
                result.add(matrix[0][i]);
            }
            return result;
        }
        // 定义状态序列
        Status[] arrs = new Status[]{Status.LeftToRight, Status.TopToBottom, Status.RightToLeft, Status.BottomToTop};
        int currentStatus = 0; // 定义状态指针
        // 初始化访问矩阵
        int[][] visited = init(matrix.length, matrix[0].length);

        int left = -1, right = matrix[0].length, top = -1, bottom = matrix.length;

        // 还有未访问的就继续
        while (hasUnVisitedElement(visited)) {
            switch (arrs[currentStatus]) {
                case LeftToRight:
                    left++;
                    top++;
                    for (int i = left; i < right - 1; i++) {
                        if(visited[top][i] == 0){
                            result.add(matrix[top][i]);
                            visited[top][i] = 1; // 访问过
                        }
                    }
                    break;
                case TopToBottom:
                    right--;
                    bottom--;
                    for (int i = top; i < bottom; i++) {
                        if(visited[i][right] == 0){
                            result.add(matrix[i][right]);
                            visited[i][right] = 1;
                        }
                    }
                    break;
                case RightToLeft:
                    for (int i = right; i > left - 1; i--) {
                        if(visited[bottom][i] == 0){
                            result.add(matrix[bottom][i]);
                            visited[bottom][i] = 1;
                        }
                    }
                    break;
                case BottomToTop:
                    for (int i = bottom - 1; i > top; i--) {
                        if(visited[i][left] == 0){
                            result.add(matrix[i][left]);
                            visited[i][left] = 1;
                        }
                    }
                    break;
            }
            currentStatus = (currentStatus + 1) % arrs.length;
        }
        return result;
    }
}

值得注意的是,需要导入ArrayList的包才行。

全部评论

相关推荐

孤寡孤寡的牛牛很热情:为什么我2本9硕投了很多,都是简历或者挂,难道那个恶心人的测评真的得认真做吗
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务