【矩阵】54.螺旋矩阵(顺时针打印矩形元素)

题目

java 复制代码
class Solution {
    public List<Integer> spiralOrder(int[][] matrix) {
        int m = matrix.length, n = matrix[0].length;
        int leftUpM = 0, leftUpN = 0, rightDownM = m - 1, rightDownN = n - 1;
        List<Integer> res = new ArrayList<>();
        while (leftUpM <= rightDownM && leftUpN <= rightDownN) {
            for (int i = leftUpN; i <= rightDownN; ++i) { // 向右
                res.add(matrix[leftUpM][i]);
            }
            for (int i = leftUpM + 1; i < rightDownM; ++i) { // 向下
            	res.add(matrix[i][rightDownN]);
            }
            for (int i = rightDownN; i >= leftUpN && leftUpM < rightDownM; --i) { // 向左
                res.add(matrix[rightDownM][i]);
            }
            for (int i = rightDownM - 1; i > leftUpM && leftUpN < rightDownN; --i) { // 向上
                res.add(matrix[i][leftUpN]);
            }
            ++leftUpM;
            ++leftUpN;
            --rightDownM;
            --rightDownN;
        }

        return res;
    }
}
相关推荐
AI科技星17 小时前
特征值与特征向量不是矩阵特殊解,是变换矩阵下不改变生长方向、仅缩放体量的固有主螺旋脉络 -《全域数学vs传统数学:人类文明进阶200讲》第73讲
人工智能·线性代数·矩阵·数据挖掘·回归·乖乖数学·全域数学
学究天人18 小时前
数学公理体系大全:Comprehensive Collection of Mathematical Axiom Systems(卷7)
线性代数·矩阵·动态规划·概率论·图论·抽象代数·拓扑学
学究天人20 小时前
数学公理体系大全:Comprehensive Collection of Mathematical Axiom Systems(卷8)
线性代数·算法·机器学习·数学建模·动态规划·图论·抽象代数
USB_ABC1 天前
视频矩阵核心技术对比:模块化vs固定机箱、硬件帧同步vs软件同步怎么选
矩阵·音视频
workflower2 天前
室内外配送机器人-应用路径
人工智能·机器学习·设计模式·矩阵·自动化
hai3152475432 天前
九章编译法----空间几何统一编译法
网络·汇编·人工智能·线性代数
lin9902122 天前
内容矩阵批量分发实战
大数据·人工智能·矩阵
学究天人2 天前
数学公理体系大全:Comprehensive Collection of Mathematical Axiom Systems(卷3.2)
线性代数·算法·机器学习·数学建模·动态规划·抽象代数·拓扑学
学究天人2 天前
数学公理体系大全:Comprehensive Collection of Mathematical Axiom Systems(卷1)
线性代数·机器学习·数学建模·动态规划·图论·抽象代数·傅立叶分析
C137的本贾尼2 天前
第8章:把向量组织起来——矩阵与线性变换
线性代数·矩阵