题解 | #螺旋矩阵#

螺旋矩阵

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

public class Solution {
    public ArrayList<Integer> spiralOrder(int[][] matrix) {
        ArrayList<Integer> res = new ArrayList<>();
        if(matrix.length == 0) return res;
        int top = 0 , 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++){
                res.add(matrix[top][i]);
            }
            top++;
            if(top > bottom) break;
            for(int i = top; i <= bottom;i++){
                res.add(matrix[i][right]);
            }
            right--;
            if(left > right) break;
            for(int i = right; i >= left;i--){
                res.add(matrix[bottom][i]);
            }
            bottom--;
            for(int i = bottom;i >= top;i--){
                res.add(matrix[i][left]);
            }
            left++;
        }
        return res;
    }
}
全部评论

相关推荐

每晚夜里独自颤抖:把华北改为华南再试一试,应该就没啥问题了。改完可能都不用投,别人主动联系了。
点赞 评论 收藏
分享
06-05 19:46
已编辑
武汉大学 后端
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务