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

1.题目描述

2.思路*

思路1:








补充2:

directions[1][0] // 表示"下"这个方向的行增量(+1)

directions[1][1] // 表示"下"这个方向的列增量(0)

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

(1) directions[0]={0,1},directions[0][0]=0,directions[0][1]=1

(2) directions[1]={1,0},directions[1][0]=1,directions[1][1]=0

(3)directions[2]={0,-1},directions[2][0]=0,directions[2][1]=-1

(4) directions[3]={-1,0},directions[3][1]=-1,directions[3][2]=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);
    }
}
相关推荐
小钻风巡山1 小时前
springboot 视频分段加载在线播放
java·spring boot·后端
不会写代码的女程序猿2 小时前
废品回收小程序:全链路数字化解决方案,赋能绿色未来
java·小程序·微信小程序定制开发
江沉晚呤时2 小时前
Redis缓存穿透、缓存击穿与缓存雪崩:如何在.NET Core中解决
java·开发语言·后端·算法·spring·排序算法
hunandede2 小时前
av_dict_get,av_dict_set,av_dict_set_int
java·前端·javascript
predisw3 小时前
垃圾收集GC的基本理解
java·jvm·算法
帅得不敢出门3 小时前
Android Framework学习二:Activity创建及View绘制流程
android·java·学习·framework·安卓·activity·window
264玫瑰资源库3 小时前
红鸟3D互动系统源码一键部署教程(含多个打包版本与功能解构)
java·数据库·游戏
黄雪超3 小时前
JVM——JVM 是如何执行方法调用的?
java·开发语言·jvm
-曾牛4 小时前
开启 Spring AI 之旅:从入门到实战
java·人工智能·spring·指南·教学·大模型应用·springai
Catfood_Eason4 小时前
XML简介
xml·java·前端