【剑指offer】19-顺时针打印矩阵

本文系《剑指offer》的刷题记录,通过牛客网在线平台测试通过。
在线测试平台:牛客网
编程资料获取:CodeLab

1-Description

输入一个矩阵,按照从外向里以顺时针的顺序依次打印出每一个数字,
例如,如果输入如下4 X 4矩阵: [ 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 ] \left[ \begin{matrix} 1 & 2 & 3 & 4 \\ 5 & 6 & 7 &8 \\ 9 & 10 &11 &12\\ 13 & 14 & 15 & 16 \end{matrix} \right] 15913261014371115481216
则依次打印出数字:
1,2,3,4,8,12,16,15,14,13,9,5,6,7,11,10.

2-Solution

本题可以通过画简图的形式加以理解,下面以下面的矩阵为例进行分析
[ 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 ] \left[ \begin{matrix} 1 & 2 & 3 & 4 \\ 5 & 6 & 7 &8 \\ 9 & 10 &11 &12\\ 13 & 14 & 15 & 16 \end{matrix} \right] 15913261014371115481216

  • 首先我们可以定义矩阵的行标和列标如下:
    在这里插入图片描述
  • 接着我们开始遍历外圈,在遍历外圈的过程中我们可以发现下标的变化规律,即可以通过四次循环来进行遍历
    在这里插入图片描述
  • 接着继续往内进行遍历,这一圈的遍历和外圈是一样的,但注意的是这里可以得到遍历的终止条件
    在这里插入图片描述

根据以上的思路,且考虑代码中的鲁棒性(即输入为空、行向量、列向量、仅一个元素的向量),可以得到下面的解决代码:

class Solution {
public:
    vector<int> printMatrix(vector<vector<int> > matrix) {
        int currow = matrix.size();//矩阵的行数
        int curcolumn = matrix[0].size();//矩阵的列数
        vector<int> printmatrix{};
        int startrow = 0;
        int startcolumn = 0;
        if(currow == 0 || curcolumn == 0) return printmatrix;//直接返回
        while(startrow < currow && startcolumn < curcolumn){//遍历终止条件
            for(int j = startcolumn; j < curcolumn; ++j)  printmatrix.push_back(matrix[startrow][j]);
            for(int i = startrow + 1; i < currow; ++i)  printmatrix.push_back(matrix[i][curcolumn - 1]);
            if(startrow < currow - 1)
                for(int j = curcolumn - 2; j >= startcolumn; --j)  printmatrix.push_back(matrix[currow - 1][j]);
            if(startcolumn < curcolumn - 1)
                for(int i = currow -2; i > startrow; --i)  printmatrix.push_back(matrix[i][startcolumn]);
            currow--;
            curcolumn--;
            startrow++;
            startcolumn++;
        }
        return printmatrix;
    }
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值