顺时针打印矩阵(C++)

顺时针打印矩阵(C++),第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]

数据范围:

0 <= matrix.length <= 100

0 <= matrix[i].length <= 100

示例1
输入:
[[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]
示例2
输入:
[[1,2,3,1],[4,5,6,1],[4,5,6,1]]
返回值:
[1,2,3,1,1,1,6,5,4,4,5,6]
思路/解法 方式一

使用标记位,顺时针输出即可。

class Solution {
public:
    enum class Dir//记录方向
    {
        Up,//向上遍历
        Down,//向下遍历
        Left,//向左遍历
        Right//向右遍历
    };
    vector<int> printMatrix(vector<vector<int>> matrix) {
        vector<int> res;//存储数据结果
        if(matrix.size() == 1 && matrix[0].size() == 0)
            return res;
        int boundaryDX = matrix.size()-1,boundaryUX = 0,//上下边界
            boundaryRY = matrix[0].size()-1,boundaryLY = 0;//左右边界
        int x = 0,y = 0;//当前位置坐标
        Dir type = Dir::Right;//默认遍历顺序
        bool flag = false;//查看是否位于一个方向的边界
        while((x <= boundaryDX && x >= boundaryUX)
             || (y <= boundaryRY && y >= boundaryLY))
        {
            if(!flag)//如果不是位于一个方向的边界位置
                res.push_back(matrix[x][y]);
            if(type == Dir::Right)//向右遍历
            {
                if(y == boundaryRY)
                {
                    type = Dir::Down;
                    boundaryUX++;
                    flag = true;
                    continue;
                }
                y++;
            }
            else if(type == Dir::Down)//向下遍历
            {
                if(x == boundaryDX)
                {
                    type = Dir::Left;
                    boundaryRY--;
                    flag = true;
                    continue;
                }
                x++;
            }
            else if(type == Dir::Left)//向左遍历
            {
                if(y == boundaryLY)
                {
                    type = Dir::Up;
                    boundaryDX--;
                    flag = true;
                    continue;
                }
                y--;
            }
            else//向上遍历
            {
                if(x == boundaryUX)
                {
                    type = Dir::Right;
                    boundaryLY++;
                    flag = true;
                    continue;
                }
                x--;
            }
            flag = false;
        }
        return res;
    }
};
方式二

旋转数组。

(1)输出原矩阵第一行
(2)删除原矩阵第一行
(3)逆时针旋转原矩阵
(4)重复上面3个步骤,直至矩阵为空

class Solution {
  public:
    /*逆时针旋转矩阵*/
    vector<vector<int>> rotationMatrix(vector<vector<int>> matrix) {
        if (matrix.empty()) return matrix;

        int col = matrix.size();
        int row = matrix[0].size();
        if (col * row == 1) return matrix;

        vector<vector<int>> result(row);

        for (int j = row - 1; j >= 0; j--) {
            for (int i = 0; i < col; i++) {
                result[row - 1 - j].push_back(matrix[i][j]);
            }
        }
        return result;
    }
    vector<int> printMatrix(vector<vector<int>> matrix) {
        vector<int> result;

        while (1) {
            if (matrix.empty()) return result;
            int col = matrix.size();
            int row = matrix[0].size();
            vector<vector<int>> tmp(col - 1);
            //将第一行输出
            for (int i = 0; i < row; i++)
                result.push_back(matrix[0][i]);
            //删去第一行
            for (int i = 1; i < col; i++) {
                for (int j = 0; j < row; j++) {
                    tmp[i - 1].push_back(matrix[i][j]);
                }
            }
            matrix = rotationMatrix(tmp);
        }
    }
};

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/langs/1323986.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-06-12
下一篇2022-06-12

发表评论

登录后才能评论

评论列表(0条)

    保存