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].

Have you been asked this question in an interview?


public class Solution {
    public List<Integer> spiralOrder(int[][] matrix) {
        List<Integer> res = new ArrayList<Integer>();
        if (matrix == null || matrix.length == 0 || matrix[0].length == 0) {
            return res;
        }
        //int m = matrix.length;
        //int n = matrix[0].length;
        int colLayers = (matrix[0].length + 1) / 2;
        int rowLayers = (matrix.length + 1) / 2;
        int rStart = 0;
        int cStart = 0;
        while (rStart < rowLayers && cStart < colLayers) {
            int rEnd = matrix.length - 1 - rStart;
            int cEnd = matrix[0].length - 1 - cStart;
            for (int j = rStart; j <= cEnd; j++) {
                res.add(matrix[rStart][j]);
            } 
            for (int j = rStart + 1; j <= rEnd; j++) {
                res.add(matrix[j][cEnd]);
            }
            if (rStart == rEnd || cStart == cEnd) {
                break;
            }
            for (int j = cEnd - 1; j >= cStart; j--) {
                res.add(matrix[rEnd][j]);
            }
            for (int j = rEnd - 1; j > rStart; j--) {
                res.add(matrix[j][cStart]);
            }
            rStart++;
            cStart++;
        }
        
        return res;
    }
}



第二次写,这样子表示更好理解一些,注意最后两层for 循环 判断条件是大于


public class Solution {
    public List<Integer> spiralOrder(int[][] matrix) {
        if (matrix == null) {
            return null;
        }
        List<Integer> list = new ArrayList<Integer>();
        if (matrix.length == 0 || matrix[0].length == 0) {
            return list;
        }
        int top = 0; 
        int left = 0;
        int bottom = matrix.length - 1;
        int right = matrix[0].length - 1;
        while(top <= bottom && left <= right) {
            for (int i = left; i <= right; i++) {
                list.add(matrix[top][i]);
            } 
            for (int i = top + 1; i <= bottom; i++) {
                list.add(matrix[i][right]);
            }
            if (bottom == top || left == right) {
                break;
            }
            //if (bottom != top) {
                for (int i = right - 1; i >= left; i--) {
                    list.add(matrix[bottom][i]);
                }
            //}
            //if (left != right) {
                for (int i = bottom - 1; i > top; i--) {
                    list.add(matrix[i][left]);
                }
            //}
            top++;
            bottom--;
            left++;
            right--;
        }
        return list;
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值