题解 | #螺旋矩阵#

螺旋矩阵

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

import java.util.*;

public class Solution {
    public ArrayList<Integer> spiralOrder(int[][] matrix) {
        ArrayList<Integer> arr = new ArrayList<Integer>();
        if(matrix.length == 0) return arr;
        int top = 0;
        int left = 0;
        int right = matrix[0].length - 1;
        int bottom = matrix.length - 1;
        while(left <= right && top <= bottom){
            for(int col = left; col <= right; col++){
                arr.add(matrix[top][col]);
            }
            top++;
            for(int row = top; row <= bottom; row++){
                arr.add(matrix[row][right]);
            }
            right--;
            if(left <= right && top <= bottom){
                for(int col = right; col >= left; col--){
                    arr.add(matrix[bottom][col]);
                }
                bottom--;
                for(int row = bottom; row >= top; row--){
                    arr.add(matrix[row][left]);
                }
                left++;
            }



        }

        return arr;
    }
}
全部评论

相关推荐

不愿透露姓名的神秘牛友
07-09 11:30
找工作7个月,投了7000封,3段世界五百强实习,才有一个offer,牛油们肯定比我强吧
码农索隆:不对不对不对,实习经历这么厉害,简历也没少投,问题出在哪呢
点赞 评论 收藏
分享
点赞 评论 收藏
分享
水墨不写bug:疑似没有上过大学
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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