前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Leetcode题目054-螺旋矩阵

Leetcode题目054-螺旋矩阵

作者头像
用户6021899
发布2022-11-18 14:12:14
2270
发布2022-11-18 14:12:14
举报

我的第一个思路:

优点:

  1. 有对称美(最大优点)
  2. 一个行(或列)循环后 指针刚好指向下一个列(或行)循环的起点。

缺点:要特别考虑只剩一个元素的情况!

我的C++代码:

代码语言:javascript
复制
class Solution 
{
public:
    vector<int> spiralOrder(vector<vector<int>>& matrix) 
    {
        int m = matrix.size();
        int n = matrix[0].size();
        vector<int> sp(m*n, 0); // m*n 个 0
        int k = 0;
        int left = 0;
        int right = n-1 ;
        int top = 0;
        int bottom = m-1;

        while(left <= right && top <= bottom)
        {
            int row = top;
            int col = left;
            if(left == right && top == bottom) 
            {   
                sp[k] = matrix[row][col]; // 防止遗漏, 仅仅一个中心的情况要特殊处理
                return sp;
            }
            while(col < right) 
            {
                sp[k++] = matrix[row][col++];
            }
            while(row < bottom)
            {
                sp[k++] = matrix[row++][col];
            }
            while(col > left and (top < bottom or col==right)) //只有一行的情况要特殊处理,防止重复保存而导致越界
            {
                sp[k++] = matrix[row][col--];    
            } 
            while(row > top and (left < right or row==bottom)) //只有一列的情况要特殊处理,防止重复保存而导致越界
            {
                sp[k++] = matrix[row--][col]; // 四季轮回,回到起点 
            } 
            left++;
            right--;
            top++;
            bottom--; // 收缩一圈
        }

        return sp;
    }
};

第二个思路:

缺点:

  1. 不对称
  2. 一个行循环后 指针跑到矩阵外,需要挪到下一个列循环的起点

优点:无需考虑只剩一个元素的情况。

我的C++代码:

代码语言:javascript
复制
class Solution
{
public:
    vector<int> spiralOrder(vector<vector<int>>& matrix) 
    {
        int m = matrix.size();
        int n = matrix[0].size();
        vector<int> sp(m*n, 0); // m*n 个 0
        int k = 0;
        int left = 0;
        int right = n-1 ;
        int top = 0;
        int bottom = m-1;

        while(left <= right && top <= bottom)
        {
            int row = top;
            int col = left;

            while(col <= right) 
            {
                sp[k++] = matrix[row][col++];
            }
            row++;
            col--; //挪到下一个列循环的起点

            while(row < bottom)
            {
                sp[k++] = matrix[row++][col];
            }

            while(top < bottom && col >= left) //只有一行的情况要特殊处理,防止重复保存而导致越界
            {
                sp[k++] = matrix[row][col--];    
            } 
            row--;
            col++; //挪到下一个列循环的起点

            while(left < right && row > top) //只有一列的情况要特殊处理,防止重复保存而导致越界
            {
                sp[k++] = matrix[row--][col]; // 四季轮回,回到起点 
            } 
            left++;
            right--;
            top++;
            bottom--; // 收缩一圈
        }

        return sp;
    }
};
本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2022-11-06,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 Python可视化编程机器学习OpenCV 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档