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

[复制链接]
发表于 2025-9-18 19:11:13 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

×
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.代码实现

方法一:不带测试用例
  1. class Solution {
  2.     public List<Integer> spiralOrder(int[][] matrix) {
  3.         //1.定义一个List,用来存储列表中的值
  4.         List<Integer> order=new ArrayList<Integer>();
  5.         //2.如果遇到是空矩阵,或者是行矩阵,或者是列矩阵,直接返回当前列表
  6.         if(matrix==null||matrix.length==0||matrix[0].length==0)
  7.         {
  8.             return order;
  9.         }
  10.         //3.计算行数,计算列数
  11.         int rows=matrix.length;
  12.         int columns=matrix[0].length;
  13.         //4.定义一个访问数组
  14.         boolean[][] visited=new boolean[rows][columns];
  15.         //5.记录总元素个数
  16.         int total=rows*columns;
  17.         //6.定义行值和列值
  18.         int row=0;
  19.         int column=0;
  20.         //7.定义一个方向数组
  21.         int[][] directions={{0,1},{1,0},{0,-1},{-1,0}};
  22.         //8.当前的方向索引先置为0,顺序是右下左上
  23.         int directionsindex=0;
  24.         //9.遍历所有元素,把原有矩阵的值先添加的orde中,并将访问位置为true.
  25.         for(int i=0;i<total;i++)
  26.         {
  27.             order.add(matrix[row][column]);
  28.             visited[row][column]=true;
  29.             int nextRow=row+directions[directionsindex][0];
  30.             int nextColumn=column+directions[directionsindex][1];
  31.               //10.如果出现越界(上下左右),或者是已访问,更新方向索引的值,让它换个方向
  32.             if(nextRow<0||nextColumn<0||nextRow>=rows||nextColumn>=columns||visited[nextRow][nextColumn]==true)
  33.             {
  34.                 directionsindex=(directionsindex+1)%4;
  35.             }
  36.              //11.更新当前行列坐标,朝当前方向继续前进
  37.             row=row+directions[directionsindex][0];
  38.             column=column+directions[directionsindex][1];
  39.         }
  40.         
  41.         //12.返回结果列表
  42.         return order;
  43.     }
  44. }
复制代码
方法二:带测试用例
  1. import java.lang.reflect.Array;
  2. import java.util.ArrayList;
  3. import java.util.List;
  4. public class H54 {
  5.     public List<Integer> spiralOrder(int[][] matrix) {
  6.         List<Integer> order=new ArrayList<Integer>();
  7.        //1.如果遇到空矩阵,或者行矩阵,或者列矩阵。直接返回当前矩阵的值
  8.        if(matrix==null||matrix.length==0||matrix[0].length==0)
  9.         {
  10.          return order;
  11.         }
  12.         //2.求当前二维矩阵总的行和列
  13.         int rows= matrix.length;
  14.         int columns=matrix[0].length;
  15.         //3.设计一个bool矩阵,用来存储参观过的元素
  16.         boolean[][] visited=new boolean[rows][columns];
  17.         //4.定义一个方向矩阵,定义从(0,0)元素开始的右下左上矩阵
  18.         int[][] directions={{0,1},{1,0},{0,-1},{-1,0}};
  19.         //5.定义一个方向的初始索引
  20.         int directionsIndex=0;
  21.         //6.计算总的矩阵元素
  22.         int total=rows*columns;
  23.         //7.定义当前的行值和列值
  24.         int row=0;
  25.         int column=0;
  26.         for(int i=0;i<total;i++)
  27.         {//9.遍历所有元素,把原有矩阵的值先添加的order中,并将访问位置为0.
  28.             order.add(matrix[row][column]);
  29.             visited[row][column]=true;
  30.             //10.更新下一步的方向
  31.             int nextRow=row+directions[directionsIndex][0];
  32.             int nextColumn=column+directions[directionsIndex][1];
  33.             if(nextRow<0||nextColumn<0||nextRow>=matrix.length||nextColumn>=matrix[0].length||visited[nextRow][nextColumn]==true)
  34.             {
  35.                 directionsIndex=(directionsIndex+1)%4;
  36.             }
  37.             //11.更新当前行列坐标,朝当前方向继续前进
  38.             row=row+directions[directionsIndex][0];
  39.             column=column+directions[directionsIndex][1];
  40.         }
  41.         return order;
  42.     }
  43.     public static void main(String[] args)
  44.     {
  45.         H54 test08=new H54();
  46.         int[][] matrix={{1,2,3},{4,5,6},{7,8,9}};
  47.         List<Integer> res=new ArrayList<Integer>();
  48.          res=test08.spiralOrder(matrix);
  49.          System.out.println("输出结果"+res);
  50.     }
  51. }
复制代码
回复

使用道具 举报

登录后关闭弹窗

登录参与点评抽奖  加入IT实名职场社区
去登录
快速回复 返回顶部 返回列表