螺旋矩阵~

简介: 螺旋矩阵~
class Solution {

    public List<Integer> spiralOrder(int[][] matrix) {

        List<Integer> order = new ArrayList<Integer>();

        if(matrix == null || matrix.length == 0 || matrix[0].length == 0){

            return order;

        }

        int rows = matrix.length;

        int colums = matrix[0].length;

        int left = 0, right = colums-1,top = 0 , bottom = rows-1;

        while(left<=right && top <= bottom ){

            for (int column = left; column <= right;column++){

                order.add(matrix[top][column]);

            }

            for(int row = top+1; row<=bottom; row++){

                order.add(matrix[row][right]);

            }

            // 检查是否需要继续遍历,如果当前区域不是一个行或列,则执行内部的遍历。

            if(left<right && top<bottom){

                //  从右到左遍历当前下边界,将元素添加到列表中

                for(int column = right-1 ; column>left ; column--){

                    order.add(matrix[bottom][column]);

                }

                for(int row = bottom; row>top; row--){

                    order.add(matrix[row][left]);

                }

            }

            // 更新边界值,缩小遍历区域。

            left++;

            right--;

            top++;

            bottom--;

        }

        //  返回按照螺旋顺序遍历矩阵后得到的整数列表

        return order;

    }

}


相关文章
|
存储 NoSQL 算法
Redis 集群模式搭建
Redis 集群模式搭建
369 5
|
存储 Linux Shell
使用Git LFS托管大文件
使用Git LFS托管大文件
|
机器学习/深度学习 存储 算法
Python 机器学习算法交易实用指南(三)(5)
Python 机器学习算法交易实用指南(三)
208 2
|
存储 IDE Java
Maven
Maven
287 0
|
XML Ubuntu Linux
探索Linux中的`busctl`命令:DBus的瑞士军刀
`busctl`是Linux下管理DBus消息总线的命令行工具,用于查看、监听和控制DBus服务。
|
安全 Linux 网络安全
渗透测试快速启动指南(全)(3)
渗透测试快速启动指南(全)
232 1
|
缓存 Java 编译器
【Java异常】Error:(19, 21) java: 无法访问org.apache.poi.xwpf.usermodel.ParagraphAlignment 找不到org.apache.po
【Java异常】Error:(19, 21) java: 无法访问org.apache.poi.xwpf.usermodel.ParagraphAlignment 找不到org.apache.po
677 0
|
开发工具 数据安全/隐私保护 iOS开发
常见报错或者警告
常见报错或者警告
483 0
常见报错或者警告
|
存储 网络协议 安全
POSIX API与网络协议栈
POSIX API与网络协议栈
175 0
|
机器学习/深度学习 算法 数据挖掘
周志华《Machine Learning》学习笔记(14)--计算学习理论
计算学习理论(computational learning theory)是通过“计算”来研究机器学习的理论
448 0
周志华《Machine Learning》学习笔记(14)--计算学习理论