LeetCode 54: Spiral Matrix

问题描述

在这里插入图片描述

思路

同剑指offer-顺时针打印矩阵只是函数名称不一样
在这里插入图片描述

https://blog.csdn.net/weixin_44135282/article/details/89683461

java实现

import java.util.ArrayList;
class Solution {
    public ArrayList<Integer> spiralOrder(int [][] matrix) {
        ArrayList<Integer> result = new ArrayList<Integer>();
        int row = matrix.length;//行数
        if(row ==0)
            return result;
        int col = matrix[0].length;//列数
        if(col==0)
            return result;
        int left=0;//左边界
        int right=col-1;//右边界
        int top=0;//上边界
        int bottom=row-1;//下边界
        while(top <= bottom && left <=right){
            for(int i=left ; i<=right ; i++)//process1
                result.add(matrix[top][i]);
            if(top+1<=bottom )//process2继续的条件 
                for(int i=top+1 ; i<=bottom ; i++)
                    result.add(matrix[i][right]);//process2
            if(top+1<=bottom && right-1>=left)//process3继续的条件
                for(int i=right-1 ; i>=left ; i--)
                    result.add(matrix[bottom][i]);//process3
            if(top+1<=bottom-1  && right-1>=left)//process4继续的条件 
                for(int i=bottom-1 ; i>=top+1 ; i--)
                    result.add(matrix[i][left]);//process4
            top++;//上边界下移1
            bottom--;//下边界上移1
            left++;//左边界右移1
            right--;//右边界左移1
        }
        return result;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值