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);
    }
}
相关推荐
Leinwin21 小时前
OpenClaw 多 Agent 协作框架的并发限制与企业化规避方案痛点直击
java·运维·数据库
薛定谔的悦21 小时前
MQTT通信协议业务层实现的完整开发流程
java·后端·mqtt·struts
enjoy嚣士1 天前
springboot之Exel工具类
java·spring boot·后端·easyexcel·excel工具类
罗超驿1 天前
独立实现双向链表_LinkedList
java·数据结构·链表·linkedlist
盐水冰1 天前
【烘焙坊项目】后端搭建(12) - 订单状态定时处理,来单提醒和顾客催单
java·后端·学习
凸头1 天前
CompletableFuture 与 Future 对比与实战示例
java·开发语言
wuqingshun3141591 天前
线程安全需要保证几个基本特征
java·开发语言·jvm
努力也学不会java1 天前
【缓存算法】一篇文章带你彻底搞懂面试高频题LRU/LFU
java·数据结构·人工智能·算法·缓存·面试
攒了一袋星辰1 天前
高并发强一致性顺序号生成系统 -- SequenceGenerator
java·数据库·mysql
小涛不学习1 天前
Spring Boot 详解(从入门到原理)
java·spring boot·后端