2025年- H18-Lc126-54.螺旋矩阵(矩阵)---java版

1.题目描述

2.思路*

思路1:








补充2:

directions10 // 表示"下"这个方向的行增量(+1)

directions11 // 表示"下"这个方向的列增量(0)

int\[\]\[\] directions = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};

(1) directions0={0,1},directions00=0,directions01=1

(2) directions1={1,0},directions10=1,directions11=0

(3)directions2={0,-1},directions20=0,directions21=-1

(4) directions3={-1,0},directions31=-1,directions32=0

3.代码实现

方法一:不带测试用例

java 复制代码
class Solution {
    public List<Integer> spiralOrder(int[][] matrix) {

        //1.定义一个List,用来存储列表中的值
        List<Integer> order=new ArrayList<Integer>();
        //2.如果遇到是空矩阵,或者是行矩阵,或者是列矩阵,直接返回当前列表
        if(matrix==null||matrix.length==0||matrix[0].length==0)
        {
            return order;
        }

        //3.计算行数,计算列数
        int rows=matrix.length;
        int columns=matrix[0].length;

        //4.定义一个访问数组
        boolean[][] visited=new boolean[rows][columns];

        //5.记录总元素个数
        int total=rows*columns;

        //6.定义行值和列值
        int row=0;
        int column=0;

        //7.定义一个方向数组
        int[][] directions={{0,1},{1,0},{0,-1},{-1,0}};

        //8.当前的方向索引先置为0,顺序是右下左上
        int directionsindex=0;

        //9.遍历所有元素,把原有矩阵的值先添加的orde中,并将访问位置为true.
        for(int i=0;i<total;i++)
        {
            order.add(matrix[row][column]);
            visited[row][column]=true;
            int nextRow=row+directions[directionsindex][0];
            int nextColumn=column+directions[directionsindex][1];
              //10.如果出现越界(上下左右),或者是已访问,更新方向索引的值,让它换个方向
            if(nextRow<0||nextColumn<0||nextRow>=rows||nextColumn>=columns||visited[nextRow][nextColumn]==true)
            {
                directionsindex=(directionsindex+1)%4;
            }
             //11.更新当前行列坐标,朝当前方向继续前进
            row=row+directions[directionsindex][0];
            column=column+directions[directionsindex][1];
        }
        
        //12.返回结果列表
        return order;

    }
}

方法二:带测试用例

java 复制代码
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.List;

public class H54 {
    public List<Integer> spiralOrder(int[][] matrix) {
        List<Integer> order=new ArrayList<Integer>();
       //1.如果遇到空矩阵,或者行矩阵,或者列矩阵。直接返回当前矩阵的值
       if(matrix==null||matrix.length==0||matrix[0].length==0)
        {
         return order;
        }

        //2.求当前二维矩阵总的行和列
        int rows= matrix.length;
        int columns=matrix[0].length;

        //3.设计一个bool矩阵,用来存储参观过的元素
        boolean[][] visited=new boolean[rows][columns];

        //4.定义一个方向矩阵,定义从(0,0)元素开始的右下左上矩阵
        int[][] directions={{0,1},{1,0},{0,-1},{-1,0}};

        //5.定义一个方向的初始索引
        int directionsIndex=0;

        //6.计算总的矩阵元素
        int total=rows*columns;

        //7.定义当前的行值和列值
        int row=0;
        int column=0;

        for(int i=0;i<total;i++)
        {//9.遍历所有元素,把原有矩阵的值先添加的order中,并将访问位置为0.
            order.add(matrix[row][column]);
            visited[row][column]=true;
            //10.更新下一步的方向
            int nextRow=row+directions[directionsIndex][0];
            int nextColumn=column+directions[directionsIndex][1];
            if(nextRow<0||nextColumn<0||nextRow>=matrix.length||nextColumn>=matrix[0].length||visited[nextRow][nextColumn]==true)
            {
                directionsIndex=(directionsIndex+1)%4;
            }
            //11.更新当前行列坐标,朝当前方向继续前进
            row=row+directions[directionsIndex][0];
            column=column+directions[directionsIndex][1];
        }
        return order;


    }
    public static void main(String[] args)
    {
        H54 test08=new H54();
        int[][] matrix={{1,2,3},{4,5,6},{7,8,9}};
        List<Integer> res=new ArrayList<Integer>();
         res=test08.spiralOrder(matrix);
         System.out.println("输出结果"+res);
    }
}
相关推荐
wuqingshun3141592 小时前
MYSQL的乐观锁和悲观锁是什么?
java
唐青枫3 小时前
Java SLF4J 实战指南:从日志门面到 Logback、MDC 和链路追踪
java
jvmind_dev3 小时前
Java GC 实战指南(番外篇):被忽视的隐形杀手 —— Class Unloading 如何拖垮 GC
java·后端
An_s3 小时前
机器学习python之识别图中物品信息
java·linux·开发语言
AI科技星3 小时前
线性算子不是空间映射函数,是全域双螺旋场之间拉伸、旋转、耦合、坍缩的跨空间标准化变换载体《全域数学vs传统数学:人类文明进阶200讲》第80讲
线性代数·算法·矩阵·数据挖掘·回归·乖乖数学·全域数学
米罗篮4 小时前
矩阵快速幂 (Exponentiation By Squaring Applied To Matrices)
c++·线性代数·算法·矩阵
AIGS0014 小时前
跨越语义鸿沟:企业本体语义平台的构建与落地
java·人工智能·python·机器学习·人工智能ai大模型应用
蓝田~5 小时前
大模型本地部署与远程调用 — 从 API 到 Agent
java·人工智能·claude·claude code
码农进化录5 小时前
Java 程序员的 AI 进化论 | Spring Boot 接入 OpenAI 的六个坑,全帮你踩了
java·spring boot·openai
疯狂成瘾者5 小时前
Java 常见集合方法
java·windows·python