小阳同学刷题日记-54. 螺旋矩阵

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

        思路:

  1. 初始化四个边界指针:top, bottom, left, right 分别表示当前螺旋遍历的上边界、下边界、左边界和右边界。
  2. 不断遍历矩阵直到所有元素都被访问过为止。在每一轮遍历中,依次遍历当前边界的元素,并将其加入结果数组中。
  3. 遍历完当前边界后,将相应的边界指针向内收缩(即对应边界向内移动一步),并检查是否还有剩余的行和列可供遍历。

        步骤: 

  1. 初始化 top = 0, bottom = m - 1, left = 0, right = n - 1
  2. 不断循环直到 top > bottom 或者 left > right
  • 遍历上边界:从 leftright
  • 遍历右边界:从 top + 1bottom
  • 遍历下边界:当 top < bottom 时,从 rightleft
  • 遍历左边界:当 left < right 时,从 bottom - 1top + 1
  • 更新边界指针:top++, bottom--, left++, right--

        代码:

class Solution {
public:
    vector<int> spiralOrder(vector<vector<int>>& matrix) {
        vector<int> result; // 存储结果的数组

        if (matrix.empty()) return result; // 处理空矩阵的情况,直接返回空数组

        int m = matrix.size(); // 矩阵的行数
        int n = matrix[0].size(); // 矩阵的列数

        int top = 0, bottom = m - 1; // 上边界和下边界的初始值
        int left = 0, right = n - 1; // 左边界和右边界的初始值

        // 按照顺时针螺旋顺序遍历矩阵
        while (top <= bottom && left <= right) {
            // 从左到右填入数字
            for (int j = left; j <= right; ++j) {
                result.push_back(matrix[top][j]);
            }
            top++; // 上边界向下移动一行

            // 从上到下填入数字
            for (int i = top; i <= bottom; ++i) {
                result.push_back(matrix[i][right]);
            }
            right--; // 右边界向左移动一列

            // 检查是否还有剩余的行和列
            if (top <= bottom && left <= right) {
                // 从右到左填入数字
                for (int j = right; j >= left; --j) {
                    result.push_back(matrix[bottom][j]);
                }
                bottom--; // 下边界向上移动一行
            }

            if (top <= bottom && left <= right) {
                // 从下到上填入数字
                for (int i = bottom; i >= top; --i) {
                    result.push_back(matrix[i][left]);
                }
                left++; // 左边界向右移动一列
            }
        }

        return result; // 返回顺时针螺旋顺序遍历的矩阵元素结果数组
    }
};

        下面这个解法是在LeetCode上面看到的:

class Solution {
public:
    vector<int> spiralOrder(vector<vector<int>>& matrix) {
        vector <int> ans;
        if(matrix.empty()) return ans; //若数组为空,直接返回答案
        int u = 0; //赋值上下左右边界
        int d = matrix.size() - 1;
        int l = 0;
        int r = matrix[0].size() - 1;
        while(true)
        {
            for(int i = l; i <= r; ++i) ans.push_back(matrix[u][i]); //向右移动直到最右
            if(++ u > d) break; //重新设定上边界,若上边界大于下边界,则遍历遍历完成,下同
            for(int i = u; i <= d; ++i) ans.push_back(matrix[i][r]); //向下
            if(-- r < l) break; //重新设定有边界
            for(int i = r; i >= l; --i) ans.push_back(matrix[d][i]); //向左
            if(-- d < u) break; //重新设定下边界
            for(int i = d; i >= u; --i) ans.push_back(matrix[i][l]); //向上
            if(++ l > r) break; //重新设定左边界
        }
        return ans;
    }
};

作者:YouLookDeliciousC
链接:https://leetcode.cn/problems/spiral-matrix/solutions/7155/cxiang-xi-ti-jie-by-youlookdeliciousc-3/
来源:力扣(LeetCode)

谢谢大家,继续努力! 

  • 5
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值