Spiral Matrix

https://leetcode.com/problems/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].

解题思路:

看代码思路应该很清楚,就是不断的去打印最外层。但是下面两点必须注意。

if(top == bottom){

            return;

        }

必须要有,否则报错。因为第三个循环打印下边的时候又会打印2。

Input: [[2,3]]
Output: [2,3,2]
Expected: [2,3]
if(left == right){

            return;

        }

必须要有,否则报错。因为第四个循环打印左侧边的时候又会打印9。

Input: [[7],[9],[6]]
Output: [7,9,6,9]
Expected: [7,9,6]

 

public class Solution {

    public List<Integer> spiralOrder(int[][] matrix) {

        List<Integer> result = new LinkedList<Integer>();

        if(matrix.length == 0){

            return result;

        }

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

        while(left <= right && top <= bottom){

            spiralEdge(result, matrix, left, right, top, bottom);

            left++;

            right--;

            top++;

            bottom--;

        }

        return result;

    }

    

    public void spiralEdge(List<Integer> result, int[][] matrix, int left, int right, int top, int bottom){

        for(int i = left; i <= right; i++){

            result.add(matrix[top][i]);

        }

        if(top == bottom){

            return;

        }

        for(int i = top + 1; i <= bottom; i++){

            result.add(matrix[i][right]);

        }

        if(left == right){

            return;

        }

        for(int i = right - 1; i >= left; i--){

            result.add(matrix[bottom][i]);

        }

        for(int i = bottom - 1; i > top; i--){

            result.add(matrix[i][left]);

        }

    }

}

 或者

public class Solution {

    public List<Integer> spiralOrder(int[][] matrix) {

        List<Integer> result = new LinkedList<Integer>();

        if(matrix.length == 0){

            return result;

        }

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

        while(left <= right && top <= bottom){

            spiralEdge(result, matrix, left, right, top, bottom);

            left++;

            right--;

            top++;

            bottom--;

        }

        return result;

    }

    

    public void spiralEdge(List<Integer> result, int[][] matrix, int left, int right, int top, int bottom){

        //打印上边

        for(int i = left; i <= right; i++){

            result.add(matrix[top][i]);

        }

        //打印右边

        for(int i = top + 1; i <= bottom; i++){

            result.add(matrix[i][right]);

        }

        if(left == right || top == bottom){

            return;

        }

        //打印下边

        for(int i = right - 1; i >= left; i--){

            result.add(matrix[bottom][i]);

        }

        //打印左边

        for(int i = bottom - 1; i > top; i--){

            result.add(matrix[i][left]);

        }

    }

}

 

你可能感兴趣的:(Matrix)