LeetCode--54. Spiral Matrix

Problem:

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

Subscribe to see which companies asked this question

Analysis:

题目理解:
    将一维或二维数组中的元素按顺时针放向进行打印;

解题分析:
    1. 模拟;
    2. 递归:
    1)每次重新计算最开始的左上初始点;
    2)然后依此遍历最上一行,最右一列,最后一行,以及最左一行;
    3)注意避免重复遍历,处理好边界问题;

Answer:
递归解法:

public class Solution {
    public List<Integer> spiralOrder(int[][] matrix) {
        List<Integer> list = new ArrayList<Integer>();
        if(matrix.length==0 || matrix[0].length==0) return list;
        int m = matrix.length, n = matrix[0].length;
        spiral_print(0,m-1,0,n-1,matrix,list);
        return list;
    }
    public static void spiral_print(int row_start,int row_end,int col_start,int col_end,int[][] matrix,List<Integer> list){
        if (row_start>row_end || col_start>col_end) return;
        for(int i=col_start;i<=col_end;i++){
            list.add(matrix[row_start][i]);
        }
        if(row_start+1>row_end) return;
        for(int i=row_start+1;i<=row_end;i++){
            list.add(matrix[i][col_end]);
        }
        if(col_start+1>col_end) return;
        for(int i=col_end-1;i>=col_start;i--){
            list.add(matrix[row_end][i]);
        }
        for(int i=row_end-1;i>row_start;i--){
            list.add(matrix[i][col_start]);
        }
        spiral_print(row_start+1,row_end-1,col_start+1,col_end-1,matrix,list);
    }
}

迭代解法:

public class Solution {

public List<Integer> spiralOrder(int[][] matrix) {
        List<Integer> result = new ArrayList<Integer>();
        if(matrix.length == 0) return result;
        int left=0,right=matrix[0].length-1,top=0,bottom=matrix.length-1,direction=0;
        while(left <= right && top <= bottom){
            if(direction == 0){
                for(int i=left;i<= right;i++){
                    result.add(matrix[top][i]);
                }
                top++;
            }
            else if(direction == 1){
                for(int i=top;i<=bottom;i++){
                    result.add(matrix[i][right]);
                }
                right--;
            }
            else if(direction == 2){
                for(int i=right;i>=left;i--){
                    result.add(matrix[bottom][i]);
                }
                bottom--;

            }
            else if(direction == 3){
                for(int i=bottom;i>=top;i--){
                    result.add(matrix[i][left]);
                }
                left++;
            }
            direction=(direction+1) % 4;
        }
    return result;
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值