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);
    }
}
相关推荐
西安邮电大学6 分钟前
Redis四大经典缓存问题
java·redis·后端·其他·面试
超梦dasgg14 分钟前
Redisson解锁失败,WatchDog会不会一直续期下去?
java·redis
Chase_______24 分钟前
【Java基础 | 11】异常处理进阶:throw、throws、自定义异常与异常链讲清楚
java·开发语言·python
DFT计算杂谈25 分钟前
VASP 磁性结构可视化:一键生成完美 VESTA / MCIF
java·前端·css·html·css3
砍材农夫31 分钟前
物联网实战:Spring Boot MQTT | 模拟器Paho客户端拆解核心点
java·javascript·网络·spring boot·后端·物联网
weixin_5394467838 分钟前
使用Java HttpServletResponse和JavaScript Fetch下载文件
java·javascript·python
我登哥MVP42 分钟前
Spring Boot 从“会用”到“精通”:自动装配原理
java·spring boot·后端·spring·tomcat·maven·intellij-idea
小的~~1 小时前
Java线程及线程池的相关的问题
java·开发语言·多线程
爱吃羊的老虎1 小时前
【JAVA】Java微服务—网关Gateway
java·微服务·gateway
人道领域1 小时前
一篇文章解决Codex的安装,实操一遍过
java·开发语言·codex