• leetcode-54. 螺旋矩阵


    1. 题目

    leetcode题目路径
    给你一个 m 行 n 列的矩阵 matrix ,请按照 顺时针螺旋顺序 ,返回矩阵中的所有元素。

    在这里插入图片描述

    输入:matrix = [[1,2,3],[4,5,6],[7,8,9]]
    输出:[1,2,3,6,9,8,7,4,5]
    
    • 1
    • 2

    在这里插入图片描述

    输入:matrix = [[1,2,3,4],[5,6,7,8],[9,10,11,12]]
    输出:[1,2,3,4,8,12,11,10,9,5,6,7]
    
    • 1
    • 2

    2. 编程

    1. 定义变量和边界:
    • 初始化矩阵的行数(m)和列数(n)。
    • 初始化结果数组,它将存储螺旋遍历的元素。
    • 定义四个边界变量 left、right、top 和 bottom,分别表示当前螺旋遍历的左、右、上和下边界。
    1. 遍历过程:
    • 使用循环,不断从外向内遍历矩阵。
    • 先从左到右遍历当前行,然后将 top 增加,表示上边界向内缩进一行。
    • 然后从上到下遍历当前列,然后将 right 减少,表示右边界向内缩进一列。
    • 继续从右到左遍历当前行,但需要检查 top 是否小于等于 bottom,以避免重复遍历。
    • 最后从下到上遍历当前列,但需要检查 left 是否小于等于 right,以避免重复遍历。
    1. 遍历结束条件:
    • 当 left 大于 right 或 top 大于 bottom 时,遍历结束,此时所有元素都已遍历。
    1. 收集元素:
    • 在每次遍历时,将遇到的元素加入结果数组。
    1. 返回结果:
    • 返回结果数组,其中包含按顺时针螺旋顺序遍历的所有元素。
    #include 
    #include 
    
    int* spiralOrder(int** matrix, int matrixSize, int* matrixColSize, int* returnSize) {
        if (matrixSize == 0) {
            *returnSize = 0;
            return NULL;
        }
        
        int m = matrixSize;
        int n = matrixColSize[0];
        int* result = (int*)malloc(m * n * sizeof(int));
        int left = 0, right = n - 1, top = 0, bottom = m - 1;
        int index = 0;
        
        while (left <= right && top <= bottom) {
            // Traverse from left to right
            for (int i = left; i <= right; i++) {
                result[index++] = matrix[top][i];
            }
            top++;
    
            // Traverse from top to bottom
            for (int i = top; i <= bottom; i++) {
                result[index++] = matrix[i][right];
            }
            right--;
    
            // Traverse from right to left
            if (top <= bottom) {
                for (int i = right; i >= left; i--) {
                    result[index++] = matrix[bottom][i];
                }
                bottom--;
            }
    
            // Traverse from bottom to top
            if (left <= right) {
                for (int i = bottom; i >= top; i--) {
                    result[index++] = matrix[i][left];
                }
                left++;
            }
        }
        
        *returnSize = m * n;
        return result;
    }
    
    int main() {
        int matrixSize = 3;
        int matrixColSize[] = {3, 3};
        int** matrix = (int**)malloc(matrixSize * sizeof(int*));
        for (int i = 0; i < matrixSize; i++) {
            matrix[i] = (int*)malloc(matrixColSize[i] * sizeof(int));
        }
    
        // Fill the matrix with values
        int value = 1;
        for (int i = 0; i < matrixSize; i++) {
            for (int j = 0; j < matrixColSize[i]; j++) {
                matrix[i][j] = value++;
            }
        }
    
        int returnSize = 0;
        int* result = spiralOrder(matrix, matrixSize, matrixColSize, &returnSize);
    
        // Print the result
        for (int i = 0; i < returnSize; i++) {
            printf("%d ", result[i]);
        }
        printf("\n");
    
        // Clean up memory
        for (int i = 0; i < matrixSize; i++) {
            free(matrix[i]);
        }
        free(matrix);
        free(result);
    
        return 0;
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
  • 相关阅读:
    c++新经典—c++基本语言
    高新技术企业研发项目立项介绍
    Bean注入方式:@Autowired、@Resource的区别
    CSS 单位解析
    YApi、Swagger
    每日OJ题_其它背包问题①_力扣474. 一和零(二维费用01背包)
    VB.net WebBrowser网页元素抓取分析方法
    http://localhost:8080打不开/shutup.bat命令行闪退
    【2】Java运行方式、程序结构以及notepad++
    maltab datenum函数与正则表达式巧用:逐日数据转为逐月数据、日序转月序
  • 原文地址:https://blog.csdn.net/weixin_47139576/article/details/133887673