• LeetCode54.螺旋矩阵


     这道题一看好像在哪做过一样,好像是写剑指offer里面的状态机的时候写过类似的,就是定义4个方向,它就是按右,下,左,上的规律螺旋的,所以只要拿4个方向给他循环就可以,我是用一个表示方向的二维数组来表示方向

    int[][] direct = new int[][]{{0,1},{1,0},{0,-1},{-1,0}};

     然后用一个corDir表示当前行进的方向,然后是用一个index来在方向数组里面循环的,它要变换为下一个方向的时候corDir=direct[index++],但是要做到循环就要让index等于4的时候给他赋值为0,这样当前方向就能在方向数组里面循环起来,

    1. int index=0;
    2. int[] corDir = direct[index];

     当你遍历到一个元素matrix[i][j]的时候,你要遍历他的下一个元素,就要用i+=corDir[0];j+=corDir[1],但是你还要判断一下i和j是不是大于等于0小于数组长度,并且还要判断这个元素是不是已经访问过,所以我们还要创建一个与matrix等大的bool数组visited,它表示对应矩阵位置中的元素是不是被访问过,这些条件只要有一个不满足我们就要换方向,变成下一个方向,然后重新计算i和j

    1. int corI = i+corDir[0]; int corJ=j+corDir[1];
    2. if(corI>=0 && corI=0 && corJ
    3. i=corI;j=corJ;
    4. }else{
    5. index++;
    6. if(index==4)index=0;
    7. corDir = direct[index];
    8. i+=corDir[0];j+=corDir[1];
    9. }

     把这些操作放到一个循环里面,每遍历一个元素就把它放进答案并且把它对应的visited改为true,但是如何终止循环呢,通过记录访问到的元素的个数,如果等于矩阵中的元素的个数就退出循环,以下是我的代码

    1. class Solution {
    2. public List spiralOrder(int[][] matrix) {
    3. List ans= new ArrayList<>();
    4. int row = matrix.length;
    5. int clown = matrix[0].length;
    6. boolean[][] visited = new boolean[row][clown];
    7. int i=0,j=0;
    8. int[][] direct = new int[][]{{0,1},{1,0},{0,-1},{-1,0}};
    9. int index=0;
    10. int[] corDir = direct[index];
    11. int visitedNum=0;
    12. while(visitedNum != row*clown){
    13. ans.add(matrix[i][j]);
    14. visited[i][j]=true;
    15. visitedNum++;
    16. int corI = i+corDir[0]; int corJ=j+corDir[1];
    17. if(corI>=0 && corI=0 && corJ
    18. i=corI;j=corJ;
    19. }else{
    20. index++;
    21. if(index==4)index=0;
    22. corDir = direct[index];
    23. i+=corDir[0];j+=corDir[1];
    24. }
    25. }
    26. return ans;
    27. }
    28. }

     看看题解做法,题解的第一种做法和我的完全一样,看来我算法写的越来越官方了,以下是题解第一种做法代码:

    1. class Solution {
    2. public List spiralOrder(int[][] matrix) {
    3. List order = new ArrayList();
    4. if (matrix == null || matrix.length == 0 || matrix[0].length == 0) {
    5. return order;
    6. }
    7. int rows = matrix.length, columns = matrix[0].length;
    8. boolean[][] visited = new boolean[rows][columns];
    9. int total = rows * columns;
    10. int row = 0, column = 0;
    11. int[][] directions = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
    12. int directionIndex = 0;
    13. for (int i = 0; i < total; i++) {
    14. order.add(matrix[row][column]);
    15. visited[row][column] = true;
    16. int nextRow = row + directions[directionIndex][0], nextColumn = column + directions[directionIndex][1];
    17. if (nextRow < 0 || nextRow >= rows || nextColumn < 0 || nextColumn >= columns || visited[nextRow][nextColumn]) {
    18. directionIndex = (directionIndex + 1) % 4;
    19. }
    20. row += directions[directionIndex][0];
    21. column += directions[directionIndex][1];
    22. }
    23. return order;
    24. }
    25. }

    题解第二种做法是按层模拟,看完第二个题解我想起来了,我做过这道题剑指offer29.顺时针打印矩阵_荔枝味啊~的博客-CSDN博客

     这种方法就是从外向内,一层一层遍历,定义一个top,bottom,right,left,4条边,每一层都是先遍历上面这条边,然后右边然后下边,然后左边,遍历完一层之后,top++,right--,bottom--,left++,循环的终止条件是左边大于了右边或者下边大于了上边,它也用用一个visited数组来记录访问过的元素,以下是题解第二种做法代码:

    1. class Solution {
    2. public List spiralOrder(int[][] matrix) {
    3. List order = new ArrayList();
    4. if (matrix == null || matrix.length == 0 || matrix[0].length == 0) {
    5. return order;
    6. }
    7. int rows = matrix.length, columns = matrix[0].length;
    8. int left = 0, right = columns - 1, top = 0, bottom = rows - 1;
    9. while (left <= right && top <= bottom) {
    10. for (int column = left; column <= right; column++) {
    11. order.add(matrix[top][column]);
    12. }
    13. for (int row = top + 1; row <= bottom; row++) {
    14. order.add(matrix[row][right]);
    15. }
    16. if (left < right && top < bottom) {
    17. for (int column = right - 1; column > left; column--) {
    18. order.add(matrix[bottom][column]);
    19. }
    20. for (int row = bottom; row > top; row--) {
    21. order.add(matrix[row][left]);
    22. }
    23. }
    24. left++;
    25. right--;
    26. top++;
    27. bottom--;
    28. }
    29. return order;
    30. }
    31. }
  • 相关阅读:
    七夕来袭!还要做CDH数据迁移怎么办?来看看DistCp
    sql中COALESCE函数详解
    web 概要(httpd2.2)
    GPT引发智能AI时代潮流
    【Unity实战技巧】教你4招计算UI物体的包围盒(Bounds)
    编码规约对于多线程的使用解读
    docker-network网络
    【javaEE】网络原理(传输层Part1)
    Scratch软件编程等级考试一级——20200913
    sql12(Leetcode1280学生们参加各科测试的次数)
  • 原文地址:https://blog.csdn.net/qq_61009660/article/details/132722544