顺时针打印矩阵

  1. 题目描述
    1. 解题思路

题目描述

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

解题思路

  • 问题具体化,仔细分析可能的情形及其处理方案

class Solution {
public:
    vector<int> printMatrix(vector<vector<int> > matrix) {
        // 矩阵不一定是正方形
        int row = matrix.size();
        int col = matrix[0].size();
        int i = 0,j = 0;
        int circle = ((row > col?col:row)-1)/2+1;
        vector<int> res;
        res.clear();
        for (int k = 0; k <= circle; ++k){
            if (res.size() < row * col)
                for (int i = k;i < col - k;++i){
                    res.push_back(matrix[k][i]);
                }
            if (res.size() < row * col) {
                for (int j = k + 1;j < row - k; ++j){
                    res.push_back(matrix[j][col - k - 1]);
                }
            }
            if (res.size() < row * col)
                for (int i = col - k - 2;i >= k; --i){
                    res.push_back(matrix[row - k - 1][i]);
                }
            if (res.size() < row * col)
                for (int j = row - k - 2;j > k; --j){
                    res.push_back(matrix[j][k]);
                }
        }
        return res;
    }
};

转载请注明来源,欢迎对文章中的引用来源进行考证,欢迎指出任何有错误或不够清晰的表达。可以在下面评论区评论,也可以邮件至 1056615746@qq.com

💰

Title:顺时针打印矩阵

Count:279

Author:攀登

Created At:2019-12-26, 23:12:31

Updated At:2024-06-15, 15:53:35

Url:http://jiafeimao-gjf.github.io/2019/12/26/sword-%E9%A1%BA%E6%97%B6%E9%92%88%E6%89%93%E5%8D%B0%E7%9F%A9%E9%98%B5/

Copyright: 'Attribution-non-commercial-shared in the same way 4.0' Reprint please keep the original link and author.

×

Help us with donation