码迷,mamicode.com
首页 > 其他好文 > 详细

[LeetCode]Spiral Matrix

时间:2015-12-02 09:15:07      阅读:103      评论:0      收藏:0      [点我收藏+]

标签:

public class Solution {
    public List<Integer> spiralOrder(int[][] matrix) {
        List<Integer> result = new ArrayList<Integer>();
        int row = matrix.length;
        if (row == 0) {
            return result;
        }
        int col = matrix[0].length;
        int level = (Math.min(row, col) + 1) / 2;
        for (int i = 0; i < level; i++) {
            for (int j = i; j < col - i; j++) {
                result.add(matrix[i][j]);
            }
            if (i == row - 1 - i) {
                break;
            }
            for (int j = i + 1; j < row - i; j++) {
                result.add(matrix[j][col - 1 - i]);
            }
            if (i == col - 1 - i) {
                break;
            }
            for (int j = col - 2 - i; j >= i; j--) {
                result.add(matrix[row - 1 - i][j]);
            }
            for (int j = row - 2 - i; j >= i + 1; j--) {
                result.add(matrix[j][i]);
            }
        }
        return result;
    }
}

 

[LeetCode]Spiral Matrix

标签:

原文地址:http://www.cnblogs.com/vision-love-programming/p/5011889.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!