《剑指offer》【剑指Offer 29.顺时针打印矩阵】

🏆个人主页企鹅不叫的博客

​ 🌈专栏

⭐️ 博主码云gitee链接:代码仓库地址

⚡若有帮助可以【关注+点赞+收藏】,大家一起进步!



💎一、题目

🏆1.题目描述

在这里插入图片描述

🏆2.原题链接

剑指Offer 29.顺时针打印矩阵

💎二、解题报告

🏆1.思路分析

🔑思路:

​ 首先用left right top bottom确定四个边界,之后按照上右下左的顺序记录元素,缩小边界

🏆2.代码详解

int* spiralOrder(int** matrix, int matrixSize, int* matrixColSize, int* returnSize){
    if(matrixSize == 0 || *matrixColSize == 0){                 //1
        *returnSize = 0;
        return NULL;
    }
    int row = matrixSize;                                       //2
    int col = matrixColSize[0];                                 //3
    int count = row*col;                                        //4
    int left = 0;                                               //5
    int right = col-1;                                          //6
    int top = 0;                                                //7
    int bottom = row-1;                                         //8
    int *a = (int*)malloc(sizeof(int)*(count+1));               //9
    *returnSize = 0;
    while(count >= 1){                                          
        for(int i = left; i <= right && count >= 1; ++i){       //10
            a[(*returnSize)++] = matrix[top][i];
            --count;
        }
        ++top;
        for(int i = top; i <= bottom && count >= 1; ++i){       //11
            a[(*returnSize)++] = matrix[i][right];
            --count;
        }
        --right;
        for(int i = right; i >= left && count >= 1; --i){       //12
            a[(*returnSize)++] = matrix[bottom][i];
            --count;
        }
        --bottom;
        for(int i = bottom; i >= top && count >= 1; --i){       //13
            a[(*returnSize)++] = matrix[i][left];
            --count;
        }
        ++left;
    }
    return a;
}

1.日常判断空
2.行
3.列
4.元素总数
5.左边界
6.右边界
7.上边界
8.下边界
9.返回数组
10.上边界循环
11.右边界循环
12.下边界循环
13.左边界循环


  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

penguin_bark

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值