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);
    }
}
相关推荐
倔强的石头10630 分钟前
【Linux指南】进程通信系列(二):匿名管道 —— pipe 的本质、fork 继承与四种读写规则
java·linux·运维
调试优选官36 分钟前
2026年IoT物联网开发赛道年度榜单深度解析:D-coding物联网系统定制与源码交付能力专项解读
java·物联网·struts·iot·开发经验
木井巳38 分钟前
【动态规划】三步问题
java·算法·leetcode·动态规划·推荐算法
小张成长计划..41 分钟前
【Linux】25:线程概念和控制
java·linux·jvm
蛋蛋的就会顺顺的10 小时前
hot100 p0——栈,贪心算法,动态规划
java·前端·javascript·算法·leetcode·力扣
需要82610 小时前
Spring Bean 生命周期:从实例化到销毁的完整钩子
java·后端·spring
小 黄 鸡10 小时前
HashMap 面试必会知识点详解(JDK 8)
java·面试·职场和发展
伯恩bourne12 小时前
Spring Boot Bean 注册的两种方式:@Component 家族 vs @Configuration + @Bean
java·spring boot·后端
silvamux_12 小时前
curl 测试 OpenAI 兼容 API 的完整流程
java·人工智能·缓存·ai·oneapi
省钱兄--zs13 小时前
深圳24小时自助健身房软硬件解决方案实战指南与开发流程详解
java·数据仓库·spring boot·小程序·架构