随笔 - 165,  文章 - 0,  评论 - 4,  阅读 - 17982

题目:

//不可以用代码随想录里螺旋矩阵的思路

class Solution {
public:
    vector<int> spiralOrder(vector<vector<int>>& matrix) {
            vector<int> result;
            if(matrix.empty()) return result;
            int rl=0,rh=matrix.size()-1;        //定义上下边界
            int cl=0,ch=matrix[0].size()-1;     //定义左右边界
            while(1){
                for(int i=cl;i<=ch;i++)result.push_back(matrix[rl][i]);  //从左到右
                if(++rl>rh)break;                                        //上边界先加一,再判断会不会超出下边界
                for(int i=rl;i<=rh;i++)result.push_back(matrix[i][ch]);  //从上到下
                if(--ch<cl)break;                                        //右边界先减一,再判断会不会超出左边界
                for(int i=ch;i>=cl;i--)result.push_back(matrix[rh][i]);  //从右到左
                if(--rh<rl)break;                                        //下边界减一判断会不会超出上边界
                for(int i=rh;i>=rl;i--)result.push_back(matrix[i][cl]);  //从下到上
                if(++cl>ch)break;
            } 
            return result;
    }
};

以上代码转自力扣评论区

posted on   孜孜不倦fly  阅读(2)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示